java-instanceof-Shortcircuit-Relational-operators
Instanceof operator :
Thread t =new Thread();
System.out.println(t instanceof thread); // true
System.out.println(t instanceof object); //true
System.out.println(t instanceof runnable); //true
System.out.println(t instanceof string); // compile time error saying convertible type found java .lang .thread Required: java. .lang .string a instanceof x
The type of a and x must be related, otherwise compile time error saying convertible type.
Ex: Object o= new Object();
System.out.println(o instanceof string); // false
Ex: null instanceof x;
The result always false
Short-circuit operator( && , || ) :
In the normal logical operator (& or |) we have to calculate both arguments compulsory, some times it may creates performance problems ,to improve the performance short-circuit operators were introduce.
Comparison between bitwise and short-circuit operators
- Bitwise operators can be applicable for both Boolean, integral types,Short - circute operators can be applicable only for Boolean type .
- Bitwise operators Both operands must be evaluated,Short - circute operators evaluation of second argument is optional.
- Bitwise operators performance is low ,Short - circute operators performance is high.
x || y ------> If x is false then y will evaluate.
Example:
int x=10,y=20;
if( (x <y)||(y gt; x/0))
{
System.out.println(“hi”); // hi
else
{
System.out.println(“hello”);
}
Equality operator:
We can use equality operators for the comparision of primitive numbers , characters,boolean values. Return type is Boolean.
Ex: 10 = = 10; // true
10 = = 10.0; // true
‘a’ = = 97 // true
- We can apply these two operators = = and!= both for primitive types and object references. In the case of primitives these operators via check magnitudes or the values.Smaller data type promote bigger data type automatically. In the case of object references double equal operator always cheeks for address comparison.i.e s1= =s2 true iff and only if both s1 and s2 pointing to the same object.
Sample s2=new sample( );
Sample s3=s1;
System.out.println (s1= =s2); // false
System.out.println (s1= =s3); // true
System.out.println (s1= =”durga”); // compile time error
- We can not use double equeal operator for different types of objects, violation leads compile time error saying incomparable types.
Ex: Sample s=new Sample();
s=null;
s = = null; // the result always false.
Ex: “java”=null ; // false
new Integer(10) = = null; // false
null==null ; // true
Ex:System.out.println( 10 = = 10 = = 10.0); // Compile time error
In the above example 10==10 returns true,true = = 10.0 ---> this shows error
System.out.println( 10 = = 10 = = true) ; // now it returns true
Relational operator ( < , < = , > , >= ) :
These operators applicable for primitives, except Boolean
Ex:10<20 ----> true
20<=50 ----> true
”Java “<”Laxman” -----> Compile time error saying operator java.lang.String,java.lang.String.
Ex:10<=true; compile time error saying can not be applied to boolean , int
Bitwise operator:
AND(&):- If both are true then only the result is true
OR( |):- if at least one is true , then only the result is true
X-OR( ^):- If both are different then only the result is true.
Ex: true & false ----> false
true | false ---> true
true ^ false----> true
Ex: 4& 5 ---> 4
4 | 5 --->5
4^ 5 ----> 1
& , | , ^ ------>
- we can apply these operator only for Boolean and integral types . we can’t apply these operator for the floating point data types.
Bitwise Compliment Operator :
We can apply bitwise compliment operator only for integral types, but not fir Boolean types i.e, ~4 valied
i.e, ~true invalied ---> Compile time error saying operator ~ can’t be applied to Boolean.
boolean compliment operator:
! true ----> false
!false -----> true
! 3 -----> compile time error saying operator ! can’t be applied to int.
conclusion:-
& , | , ^ -------------> we can apply both integral and Boolean.
~ -----------------> only for integral type
! -----------------> only for Boolean type.
Conditional operator :
The only available ternary operator is the conditional operator.
int a=Boolean expression ? value if true : value if false;
int a=(true)?10:12; //10
We can perform nesting of conditional operator also possible.
Ex:- int a=( false) ? 10 : ( true) ? 20 : 40 ; )
Ex:- byte x=( a <>
New operator:
This can be used for create of objects.
new and instanceof are acts as both keywords and operators.
[ ]-----> This can be used for declaration and construction of arrays.
Assignment operator:
(1)simple assignment:
int x=10;
int y=x;
(2) compound assignment:-
In the case of compound assignment operator compiler will take care about type casting problems . i.e it can perform internal automatic typecasting .
i.e Ex:- byte a=10;
a+=10;
System.out.println(a); // 20
Ex:- byte a=10;
a=a+10;
System.out.println(a); //CE saying PLP.
The following are all possible compound assignment operator :
=,+=,-=,*=,%=,>>=,<<=,>>>=,&=,|=,^=,!=
(3)chained assignment operator:-
Ex:- int a ,b ,c ,d ;
int a=b=c=d=40;//valid
int a=b=c=d=40; //invalid
Why because chained assignment is not allowed at the time of declaration.
Ex: int a ,b ,c ,d;
a=b=c=d=40;
a+=b*c/d-=10;
System.out.println(a+”……..”+b+”….”+c+”……”+d); //80….40…..1….30
Precedence of java operator:
(1)Unary ---------- [], x++, x--,++x,--x, ~,!,new,
(2)arithmetic -----*,/,%,+,-
(3) shift operator --- >>,<=,>,>=,>>>
(4)comparison:-<,<=,>,>=,instsnceof
(5)equality:- ==,!=
(6) bitwise :- &,^ ,|
(7) short-circuit:- &&,||
(8) Conditional : - ?,:
(9) assignment:-=,+=,-=,*=,%=,>>=,<<=,>>>-,&=,|=,^=,!=
Evaluation order of operators:
class EvaluationOrder
{
public static void main(String arg[])
{
System.out.println(m1(1) +m1(2)*m1(3)/m1(4)-m1(5)* m1(6));
}
public static int m1(int i)
{
System.out.println(i);
return i;
}
}
1. 1+2*3/4-5*6
2. 1+6/4-5*6
3. 1+1-5*6
4. 1+1-30= -28(result)
- Before applying any operator , first we have to evaluate all the operands .The order of evaluation of operands is always from left to right . And then after evaluation of operands we have to apply the operators according to precedence
0 comments:
Post a Comment