+ 3

Single and Double Or and And Operators

What is the difference between | and || and ; & and &&

25th Apr 2018, 10:40 PM
Sai Nikhil Paruchuru
Sai Nikhil Paruchuru - avatar
2 Answers
+ 3
Though their function is same, there is a small difference in the way they are executed. Incase of & operator, It evaluates both sides of the operator ,while && evaluates the right side of the operator only if left side returns true. Similarly, The | evaluates both sides of the operator ,while || evaluates the right side of the operator only if left side returns false. Example: The output of the following line of code is 0, int a =0,b=1; if(a==1&--b>0) System.out.println(b); However, the out of the following code is 1, int a=0,b=1; if(a==1&&--b>0) System.out.println(b);
26th Apr 2018, 10:55 AM
Sai Nikhil Paruchuru
Sai Nikhil Paruchuru - avatar