Tuesday, January 3, 2017

short circuit operators in java

Short-Circuit Operators:

----------------------------------------------------------------------------------------------------------
The main intention of short circuit operator is to improve java application performance.

There are two types of short-circuit operators in java:

1. ||(double or) 2. &&(double and)

Q. what is the difference between | and ||?
Ans:
------
In case of 'logical-OR' operator, if first operand value is "true" then it is not required to check second operand value, directly we can predict the result of "OR" operator is "true".

If First operand value is "false" then it is mandatory to evaluate second operand value.

In case of  '|' operator, even though first operand value is true, still JVM will evaluate second operand value in order to get the overall expression
result, here evaluating second operand is unnecessary, it will increase application execution time and it will reduce application performance.

In case of '||' operator, if first operand value is "true", then JVM will predict the overall expression result is "true", without evaluating
second operand value, it will reduce application execution time & it will improve application performance.

EX:
class  Test
{
public static void main(String[] args)
{
int a = 10;
int b = 10;
if((a++ == 10) | (b++ == 10))
{
System.out.println(a+"  "+b);
}

int c = 10;
int d = 10;
if((c++ == 10) || (d++ == 10))
{
System.out.println(c+"  "+d);
}
}
}

Output:
D:\New folder>java Test
11  11
11  10

Note: Here you can see, in the First operation, even the first operand is true, still |(single or) perform both operand while || perform only single operand and display output.
--------------------------------------------------------------------------------------

Q. what is the difference between & and &&?
Ans:
------
In case of 'logical-AND' operator, if first operand value is "false" then it is not required to check second operand value, directly we can predict the result of "AND" operator is "false".

If First operand value is "true" then it is mandatory to evaluate second operand value.

In case of '&' operator, even though first operand value is false, still JVM will evaluate second operand value in order to get the overall expression
result, here evaluating second operand is unnecessary, it will increase application execution time and it will reduce application performance.

In case of '&&' operator, if first operand value is "false", then JVM will predict the overall expression result is "false", without evaluating
second operand value, it will reduce application execution time & it will improve application performance.

EX:
class  Test
{
public static void main(String[] args)
{
int a = 10;
int b = 10;
if((a++ != 10) | (b++ != 10))
{
}
System.out.println(a+"  "+b);

int c = 10;
int d = 10;
if((c++ == 10) || (d++ == 10))
{
}
System.out.println(c+"  "+d);
}
}

Output:
D:\New folder>java Test
11  11
11  10

Note: Here you can see, in the First operation, even the first operand is false, still &(single AND) perform both operand while && perform only single
operand and display output.
--------------------------------------------------------------------------------

No comments:

Post a Comment