Java Programming Reference Material Unit-I: Sree Venkateswara College of Engineering N.Rajupalem Nellore
Java Programming Reference Material Unit-I: Sree Venkateswara College of Engineering N.Rajupalem Nellore
OF ENGINEERING
N.RAJUPALEM
NELLORE
JAVA PROGRAMMING
Reference Material
Unit-I
Unit - I
INTRODUCTION
Java is an object oriented programming language developed by James Gosling & his crew.
Previously it was called OAK but its renamed to java during 1995.
Many versions evolved over the years now the current version is java JDK 8.
KEY ATTRIBUTES OF OOP
In computer science, computer languages adopt particular programming methodology to write
programs.
For example, C language uses structured programming, Assembly language uses non structured
whereas java uses object oriented programming.
All java programs are written using object oriented programming concepts.
The oop concepts or key attributes of oop are encapsulation, inheritance, and polymorphism.
Encapsulation:
Encapsulation is the mechanism of hiding the internal details and allowing a simple interface
which ensures that the object can be used without having to know its internal details.
Example: A swipe machine encapsulates/hides internal circuitry from all users and provides
simple interface for access by every user.
In java, encapsulation is achieved by binding data and methods in a single entity called class
and hiding data behind methods and allowing the interface with the public methods of class.
The following program demonstrates the concept of encapsulation.
In the above program data and methods are encapsulated in a class called Customer and data
is hidden from user by declaring them to private and interface is allowed to setter and getter
methods.
Inheritance:
Inheritance is a mechanism by which we can define generalized characteristics and behavior
and also create specialized ones. The specialized ones automatically inherit all the properties
of generalized ones.
The process of getting the output for above program requires 3steps.
Enter the source code in either editor like notepad or IDE like netbeans or eclipse and save the
program name as Demo.java
Compile the program using javac command followed by Demo.java in command prompt
which generates .class file.
Execute the program using java command followed by Demo which will generates the output.
Description about each part of a program:
The first line import the classes required for the program. The classes required will be in the
subdirectory lang of main directory java. Since the above program requires print() method of
system class the package has to be imported into the program.
The statements written between /* */ are called multiline comments which will be ignored by
compiler. It explains the operation of program to anyone reading the program.
Since java is oop language, every java program is written within a class. So we use class
keyword followed by class name Demo.
A class code starts with a { and ends with }. In between { } we can write variables and
methods.
main() is the method which is the starting point for JVM to start execution.
Since JVM calls the main() method there wont be nothing for the main() to return to JVM.
So it is declared as void type.
Since main() method should be available to JVM which is an outside program it should be
declared with an access specifier public. Otherwise it is not accessible to JVM.
The keyword static allows main() to be executed without creating an object for class Demo.
This is necessary because main() is executed by JVM before any objects are made.
To print something to the console output we use System.out.print() method. System is a class
and out is a variable in it. Print() method belongs to PrintStream class.
When we call out variable PrintStream class object will be created internally. So we write
System.out.print().
To print a string to the output we use and passed as a parameter to print() method.
Elements of a Java Program
Name
Description
Parenthesis
{}
Braces
[ ]
;
,
Brackets
Semicolon
Comma
Period
KEYWORDS: There are 50 reserved keywords currently defined in the Java language. These
keywords, combined with the syntax of the operators and separators, form the definition of the
Java language. These keywords cannot be used as names for a variable, class, or method.
In addition to the keywords, java reserves true, false and null. Const and goto keywords are
reserved and meant for future use.
OPERATORS
An operator is a symbol which tells the compiler to perform a particular operation on
operands. Ex: a + b. a and b are operands + is an operator meant for addition operation.
Categories of operators:
Arithmetic operators
Assignment operator
Unary operators
Relational operators
Logical operators or short circuit operators
Boolean operators
Bitwise operators
Ternary or conditional operator
Member operator
Instanceof operator
New operator
Cast operator.
Arithmetic operators:
Operator
Operation
example
+
Addition,
string 12+10;
String s2=one,s3=two;
concatenation
String s1=s2+s3;
Subtraction
Int x=4,y=3,z; z=y-x;
*
Multiplication
Int x=4,y=3,z; z=y*x;
/
Division
Int x=4,y=3,z; z=y/x;
%
Modulus operator
Int x=4,y=3,z; z=y%x;
(gives remainder)
Assignment operator:
This is used to store a value or variable or an expression into a variable.
Ex: int x=3, y=5, z;
z= x; z=x/3+y;
Unary operators:
Operator
Operation
Example
Unary minus(negates given Int x=5;
System.out.print(-x));
value).
++
Increments a variable value Int x=5;
System.out.print(x++);
by 1.
-decrements a variable value Int x=5;
System.out.print(x--);
by 1.
Preincrement operator allows incrementation first and then other operations are performed.
Ex: int x=10;
System.out.print(++x);// gives result 11 because incrementation first happens & then
print operation happens.
Postincrement operator allows other operations to be performed first and then incrementation
happens.
Ex: int x=10;
System.out.print(x++);// gives result 10 because print operation happens
Boolean operators:
These operators act on Boolean variables and produce Boolean type result.
Operators
&(Boolean and operator)
|(Boolean or operator)
Operation
Returns true if both variables
true. Otherwise false returns.
Returns true if any variable
is true. Otherwise false
returns.
Converts true to false and
vice-versa.
Example(a=true b=false)
System.out.print(a&b)//returns
false
System.out.print(a|b)//returns
true
System.out.print(!(a|b))//returns
false
Bitwise operators:
These operators act on individual bits of a number and produce the appropriate result.
Operators
Operation
Example x=10, y=11
~(Bitwise Complement Operator) Gives complement of (~x)=-11
a given number
& (Bitwise and Operator )
Gives 1 if both bits System.out.print(x&y);//returns
are 1. Otherwise false 00001010
returns
|(Bitwise or Operator)
Gives 1 if any of the System.out.print(x|y);//returns
bits are 1. Otherwise 00010100
false returns.
^(Bitwise xor Operator)
Gives 1 if odd System.out.print(x^y);//returns
number of 1s present 00000001
in input. Otherwise
false returns.
<<(Bitwise left shift operator)
Shifts bits to the left System.out.print(x<<2);//returns
with the specified 00101000
number of times.
>>(Bitwise right shift operator)
Shifts bits to the right System.out.print(x>>2);//returns
with the specified 00000010
number of times.
>>>(Bitwise zero fill right shift Shifts bits to the right
operator)
with the specified
number of times and
fill left side empty
places with zeroes.
Ternary Operator or Conditional Operator
This acts an an alternative for if else statement.
Syntax: variable =expression1? expression2:expression3;
Example: max = (a>b)? a :b;
If exp1 evaluates to true exp2 will be executed. Otherwise exp3 will be executed.
Member operator (.)
This is used in 3ways.
Used when refer to subpackage and class of a package.
Example: import javax.swing.JOptionPane;
Used when refer to variable of a class or object.
Example: System.out
Used when refer to method of a class or object.
Example: Math.sqrt(23);
instanceof Operator
This is used to test if an object belongs to a (class or interface) or not.
Syntax: Boolean variable= object instanceof class;
Example: Boolean x= custobj instanceof customer;
New operator
new operator is often used to create objects to classes.
Syntax: classname obj=new classname();
Example: customer custobj=new customer();
Cast operator
Cast operator is used to convert one data type into another data type.
Syntax: datatype target-var=(target-datatype)variable;
Example: int x;
float y=24.5678;
x=(int)y;
Priority of operators
In an expression some of the operators will execute first and some operators will execute next.
To determine which operators execute first priority will be assigned to operators.
The priority is as follows.
priority
operators
st
1
() , []
nd
2
++ , -rd
3
*, /, %
4th
+, th
5
Relational operators
th
6
Boolean
and
bitwise
operators
7th
Logical operators
th
8
Ternary operator
th
9
Assignment operator
DATATYPES
Datatype is an identified type for any data being used in a program.
This is used to determine the following.
o Type of data stored in a variable
o No of bytes it occupies in memory
o Range of data.
In java datatypes are divided into 2 categories.
Primitive datatypes
Non primitive datatypes
Under primitive datatypes we have 8 datatypes. Under non primitive we have class, array,
interface.
Default
value
0
0
0
0
0.0f
0.0d
Null
false
example
byte b=10;
short s=23451;
int i=2345666;
long l=342156789;
Float f=3.45f;
double d=3.2134d;
char ch=x;
boolean b=true;
Float can represent upto 7 digits accurately after decimal point, whereas double can represent
upto 15 digits accurately after decimal point.
char datatype can support 65536 characters including all human language characters which is
there in the Unicode system.
VARIABLE
A variable is an identifier for the data in the program.
It holds data in a program
It is named location in memory whose value changes during program execution.
The type of data stored in the variables can be specified with datatype.
Syntax: datatype variable-name=value;
Example: int x=23456;