Short Circuite Operators part 4

Short Circuite Operators part 4

                                                             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.
Double Short Circuit Operator(&& ,||)

  • the second arguments evaluation is optional.
example-:
      x && y  => y will evaluated if x is true iff x is false then y won't be evaluated.
      x ||  y =>  y will be evaluated  if x is false iff x is true y won't be evaluated.
  • 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.

Example-:
/*
int x='a';
System.out.print(x);//95
//compiler converts char to int automatically by implicit typecasting

double x=10;
System.out.print(x);//10.0
//compiler converts int to double automatically by implicit typecasting

*/


                                                     Explicit TypyCasting
  • 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.
the following are various possibilities where explicit typeCasting is required.


Left ---------> Right (Implicit TypeCasting)
Right-------->Left (Explicit TypeCasting)

whenever assigning bigger data type value to smaller data type value by explicit typeCasting the most significant bits will be last we have to consider only LSB(list significant bits).

Example-:
/*
int a=10;
byte b=a;
System.out.print(b);//compile time error-: possible loss of precision found int required byte.

a=130
a=00000000000000000000000010000010

byte = 8bit
8bit consider from last.

(byte)a=10000010

MSB=1
remaining bits are converted into 2's complement
2's =1111101
convert into 1's complement
1's=    1111101
                    +1
           ______
            1111110
1111110=126
but MST is 1 and 1 means negative
Ans= -126


int a=130;
byte b=(byte)a;
System.out.print(b);//-126

*/

Example 2-:
/*
  int x=150;
  short s=(short)x;
  System.out.print(s);//150
  byte b=(byte)x;
  System.out.print(b);//-106
*/
if a assign floating point value to the integral type by explicit typeCasting the digit after the decimal point will be loss.

Example
/*
double d=130.456;
int x=(int)d;
System.out.print(x)//130
byte b=(byte)d;
System.out.print(b);//-126
*/

Previous Post Next Post

Most Recent