Short Circuit Operators
In Java logical operators, if the evaluation of a logical expression exits in between before complete evaluation, then it is known as Short-circuit. A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing.
1-: AND(&&)
2-:OR(||)
these are exactly same as bitwise operators (&,|) except the following differences.
Single Short Circuit Operator(&,|)
- Both arguments Should be evaluated always.
- relatively performance is low.
- Applicable for both boolean and Integral types.
- the second arguments evaluation is optional.
- relatively performance is high.
- Applicable only for boolean but not for integral.
example-:
/*
1 -:value for single Operator (&)
int x=10,y=15;
if(++x<10 & ++y>15){
x++;
}else{
y++;
}
System.out.print(x+" "+y);
2 -:value for single Operator (|)
if(++x<10 | ++y>15){
x++;
}else{
y++;
}
System.out.print(x+" "+y);
3-:value for double short circuit operator (&&)
if(++x<10 && ++y>15){
x++;
}else{
y++;
}
System.out.print(x+" "+y);
4-:value for double short circuit operator (||)
if(++x<10 || ++y>15){
x++;
}else{
y++;
}
System.out.print(x+" "+y);
Ans-:
___ _|__x____|______y______|____
& | 11 | 17 |
______________________________
&& | 11 | 16 |
______________________________
| | 12 | 16 |
______________________________
|| | 12 | 16 |
______________________________
*/
Example 2-:
/*
int x=10;
if(++x<10 && (x/0 <10)){
System.out.print("Hello");
}else{
System.out.print("Hi");
}
Ans-: "Hi"
if we replace with(&) then we will get run time exception arithmetic exception x divide by 0
*/
TypeCasting Operators
there are two types of typeCasting operators.
1-: Implicit type casting
2-: Explicit type casting
Implicit typecasting
- compiler is responsible to perform implicit typecasting.
- when ever we assign a smaller data type value to a bigger data type variable implicit typecasting will be performed.
- it is also known as widening or upcasting
- there is no lass of information in this typecasting.
- the following are various possible conversions where implicit typecasting will be performed.
- programmer is responsible to explicit typeCasting.
- whenever assigning bigger data type value to a smaller data type variable then explicit typeCasting is required
- it is also known as narrowing are down casting.
- there may be a chance of lass of information in this type casting.