Operators & Assignments
Operators & Assignments
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
59 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
pre-increment ex : y=++x ;
Increment Operator
post-increment ex: y=x++;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
60 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
pre-decrement ex : y=--x ;
Decrement Operator
post-decrement ex : y=x-- ;
The following table will demonstrate the use of increment and decrement operators.
Expression initial value of x value of y final value of x
y=++x 10 11 11
y=x++ 10 10 11
y=--x 10 9 9
y=x-- 10 10 9
Ex :
1. Increment & decrement operators we can apply only for variables but not for
constant values.other wise we will get compile time error .
Ex :
int x = 4;
int y = ++x;
System.out.pritnln(y); //output : 5
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
61 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Ex 2 :
int x = 4;
int y = ++4;
System.out.pritnln(y);
int x= 4;
int y = ++(++x);
System.out.println(y);
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
62 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
3. For the final variables we can't apply increment or decrement operators ,other
wise we will get compile time error
Ex:
final int x = 4;
x++; // x = x + 1
System.out.println(x);
4. We can apply increment or decrement operators even for primitive data types
except boolean .
Ex:
int x=10;
x++;
System.out.println(x); //output :11
char ch='a';
ch++;
System.out.println(ch); //b
double d=10.5;
d++;
System.out.println(d); //11.5
boolean b=true;
b++;
System.out.println(b);
CE : operator ++ can't be applied to boolean
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
63 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
If we are applying any arithmetic operators b/w 2 operands 'a' & 'b' the result type is
max(int , type of a , type of b)
Ex 1:
byte a=10;
byte b=20;
byte c=a+b; //byte c=byte(a+b); valid
System.out.println(c);
Ex:
byte b=10;
b++;
System.out.println(b); //output : 11
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
64 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Arithmetic Operator :
1. If we apply any Arithmetic operation b/w 2 variables a & b ,
the result type is always max(int , type of a , type of b)
2. Example :
3.
4. byte + byte=int
5. byte+short=int
6. short+short=int
7. short+long=long
8. double+float=double
9. int+double=double
10. char+char=int
11. char+int=int
12. char+double=double
13.
14. System.out.println('a' + 'b'); // output : 195
15. System.out.println('a' + 1); // output : 98
16. System.out.println('a' + 1.2); // output : 98.2
17. In integral arithmetic (byte , int , short , long) there is no way to represents
infinity , if infinity is the result we will get the ArithmeticException / by zero
System.out.println(10/0); // output RE : ArithmeticException / by zero
But in floating point arithmetic(float , double) there is a way represents infinity.
System.out.println(10/0.0); // output : infinity
For the Float & Double classes contains the following constants :
1. POSITIVE_INFINITY
2. NEGATIVE_INFINITY
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
65 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
18. NaN(Not a Number) in integral arithmetic (byte , short , int , long) there is no
way to represent undefine the results. Hence the result is undefined we will get
ArithmericException in integral arithmetic
System.out.println(0/0); // output RE : ArithmeticException / by zero
But floating point arithmetic (float , double) there is a way to represents
undefined the results .
For the Float , Double classes contains a constant NaN , Hence the result is
undefined we won't get ArithmeticException in floating point arithmetics .
System.out.println(0.0/0.0); // output : NaN
System.out.println(-0.0/0.0); // output : NaN
19. For any 'x' value including NaN , the following expressions returns false
20. // Ex : x=10;
21. System.out.println(10 < Float.NaN ); // false
22. System.out.println(10 <= Float.NaN ); // false
23. System.out.println(10 > Float.NaN ); // false
24. System.out.println(10 >= Float.NaN ); // false
25. System.out.println(10 == Float.NaN ); // false
26. System.out.println(Float.NaN == Float.NaN ); // false
27.
28. System.out.println(10 != Float.NaN ); //true
29. System.out.println(Float.NaN != Float.NaN ); //true
30. ArithmeticException :
1. It is a RuntimeException but not compile time error
2. It occurs only in integral arithmetic but not in floating point arithmetic.
3. The only operations which cause ArithmeticException are : ' / ' and ' % '
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
66 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Example :
Example :
6. Example :
a=b+c+d ;
CE : incompatible type
found : int
required : java.lang.String
7. Example :
8.
a=a+b+c ; // valid
9. Example :
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
67 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
10.
b=a+c+d ;
11.
12.
CE : incompatible type
13.
found : java.lang.String
14.
required : int
15. Example :
16.
b=b+c+d ; // valid
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
68 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
12. To use the equality operators between object type compulsory these should be
some relation between argument types(child to parent , parent to child) ,
Otherwise we will get Compiletime error incompatible types
13. Thread t=new Thread( ) ;
14. Object o=new Object( );
15. String s=new String("durga");
16. System.out.println(t ==o); //false
17. System.out.println(o==s); //false
18. System.out.println(s==t);
19. CE : incompatible types : java.lang.String and java.lang.Thread
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
69 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
20. For any object reference of on r==null is always false , but null==null is always
true .
21. String s= new String("ashok");
22. System.out.println(s==null); //output : false
23. String s=null ;
24. System.out.println(r==null); //true
25. System.out.println(null==null); //true
26. What is the difference between == operator and .equals( ) method ?
In general we can use .equals( ) for content comparision where as == operator
for reference comparision
27.
28. String s1=new String("ashok");
29. String s2=new String("ashok");
30. System.out.println(s1==s2); //false
31. System.out.println(s1.equals(s2)); //true
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
70 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
instanceof operator :
1. We can use the instanceof operator to check whether the given an object is
perticular type or not
Ex :
public class Thread extends Object implements Runnable {
}
16. To use instance of operator compulsory there should be some relation between
argument types (either child to parent Or parent to child Or same type)
Otherwise we will get compile time error saying inconvertible types
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
71 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
17.
18. Thread t=new Thread( );
19. System.out.println(t instanceof String);
20. CE : inconvertable errors
21. found : java.lang.Thread
22. required : java.lang.String
23. Whenever we are checking the parent object is child type or not by using
instanceof operator that we get false.
24. Object o=new Object( );
25. System.out.println(o instanceof String );
//false
26.
27. Object o=new String("ashok");
28. System.out.println(o instanceof String); //true
29. For any class or interface X null instanceof X is always returns false
30. System.out.println(null instanceof X); //false
Example:
System.out.println(true&false);//false
System.out.println(true|false);//true
System.out.println(true^false);//true
We can apply bitwise operators even for integral types also.
Example:
System.out.println(4&5);//4 using binary digits
System.out.println(4|5);//5 4-->100
System.out.println(4^5);//1 5-->101
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
72 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Example :
2. Example :
3. System.out.println(~true); // CE :opetator ~ can not be applied to
boolean
4. System.out.println(~4); //-5
5.
6. description about above program :
7. 4--> 0 000.......0100 0-----+ve
8. ~4--> 1 111.......1011 1--- -ve
9.
10. 2's compliment of ~4 --> 000....0100 add 1
11. result is : 000...0101 =5
12. Note : The most significant bit access as sign bit 0 means +ve number , 1 means -
ve number.
+ve number will be represented directly in memory where as -ve number will be
represented in 2's comlement form.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
73 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Example :
Example:
System.out.println(!true);//false
System.out.println(!false);//true
System.out.println(!4);//CE : operator ! can not be applied to int
Summary:
&
| Applicable for both boolean and integral types.
^
~ --------Applicable for integral types only but not for boolean types.
! --------Applicable for boolean types only but not for integral types.
x&&y : y will be evaluated if and only if x is true.(If x is false then y won't be evaluated
i.e., If x is ture then only y will be evaluated)
x||y : y will be evaluated if and only if x is false.(If x is true then y won't be evaluated i.e.,
If x is false then only y will be evaluated)
Example :
int x=10 , y=15 ;
if(++x < 10 || ++y > 15) { //instead of || using &,&&, |
operators
x++;
}
else {
y++;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
74 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
System.out.println(x+"----"+y);
Output:
operator x y
& 11 17
| 12 16
&& 11 16
|| 12 16
Example :
int x=10 ;
if(++x < 10 && ((x/0)>10) ) {
System.out.println("Hello");
}
else {
System.out.println("Hi");
}
output : Hi
1. implicit
2. explicit
6.
7. Example 1:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
75 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
8. int x='a';
9. System.out.println(x);//97
10. Note: Compiler converts char to int type automatically by implicit type casting.
11. Example 2:
12. double d=10;
13. System.out.println(d);//10.0
6.
7. Example :
8.
9. int x=130;
10. byte b=(byte)x;
11. System.out.println(b); //-126
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
76 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
12.
13. Example 2 :
14.
15. int x=130;
16. byte b=x;
17. System.out.println(b); //CE : possible loss of precision
18. When ever we are assigning higher datatype value to lower datatype value
variable by explicit type-casting ,the most significant bits will be lost i.e., we have
considered least significant bits.
19. Example 3 :
20.
21. int x=150;
22. short s=(short)x;
23. byte b=(byte)x;
24. System.out.println(s); //150
25. System.out.println(b); //-106
26. When ever we are assigning floating point value to the integral types by explicit
type casting , the digits of after decimal point will be lost .
27. Example 4:
28.
29. double d=130.456 ;
30.
31. int x=(int)d ;
32. System.out.println(x); //130
33.
34. byte b=(byte)d ;
35. System.out.println(b); //-206
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
77 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Assignment Operator :
There are 3 types of assignment operators
1. Simple assignment:
Example: int x=10;
2. Chained assignment:
3. Example:
4. int a,b,c,d;
5. a=b=c=d=20;
6. System.out.println(a+"---"+b+"---"+c+"---"+d);//20---20---20---20
7. int b , c , d ;
8. int a=b=c=d=20 ; //valid
Example 2:
int a=b=c=d=30;
CE : can not find symbol
symbol : variable b
location : class Test
9. Compound assignment:
1. Sometimes we can mixed assignment operator with some other operator
to form compound assignment operator.
2. Ex:
3. int a=10 ;
4. a +=20 ;
5. System.out.println(a); //30
6. The following is the list of all possible compound assignment operators in
java.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
78 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
byte b=10;
b=b+1;
System.out.println(b);
byte b=10;
b++;
CE :
System.out.println(b); //11
possible loss of precission
found : int
required : byte
byte b=127;
byte b=10;
b+=3;
b+=1;
System.out.println(b);
System.out.println(b); //11
//-126
Ex :
int a , b , c , d ;
a=b=c=d=20 ;
a += b-= c *= d /= 2 ;
System.out.println(a+"---"+b+"---"+c+"---"+d);// -160...-180---200---10
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
79 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Conditional Operator (? :)
The only possible ternary operator in java is conditional operator
Ex 1 :
int x=(10>20)?30:40;
System.out.println(x); //40
Ex 2 :
int x=(10>20)?30:((40>50)?60:70);
System.out.println(x); //70
new operator :
1. We can use "new" operator to create an object.
2. There is no "delete" operator in java because destruction of useless objects is the
responsibility of garbage collector.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
80 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
[ ] operator:
We can use this operator to declare under construct/create arrays.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
81 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
Ex 2:
int i=1;
i+=++i + i++ + ++i + i++;
System.out.println(i); //13
description :
i=i + ++i + i++ + ++i + i++ ;
i=1+2+2+4+4;
i=13;
new Vs newInstance( ) :
1. new is an operator to create an objects , if we know class name at the beginning
then we can create an object by using new operator .
2. newInstance( ) is a method presenting class " Class " , which can be used to
create object.
3. If we don't know the class name at the beginning and its available dynamically
Runtime then we should go for newInstance() method
4. public class Test {
5. public static void main(String[] args) Throws Exception {
6. Object o=Class.forName(arg[0]).newInstance( ) ;
7. System.out.println(o.getClass().getName( ) );
8. }
9. }
10. If dynamically provide class name is not available then we will get the
RuntimeException saying ClassNotFoundException
11. To use newInstance( ) method compulsory corresponding class should contains
no argument constructor , otherwise we will get the RuntimeException saying
InstantiationException.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
82 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
83 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com
Core Java with SCJP/ OCJP Notes By Durga Sir Operators & Assignments
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038,
84 040 – 64 51 27 86, 80 96 96 96 96, 9246212143 | www.durgasoft.com