Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I always use || instead of | and && instead of &?

Since I started programming Java, I've noticed that everyone was using && and || instead of & and |. What is the reason for this? I've been using && and || all this time because I didn't know you can use & and | on booleans.

class A{static{
  boolean t = true;
  boolean f = false;
  System.out.println("&ff " + (f&&f) + " " + (f&f));
  System.out.println("&ft " + (f&&t) + " " + (f&t));
  System.out.println("&tf " + (t&&f) + " " + (t&f));
  System.out.println("&tt " + (t&&t) + " " + (t&t));
  System.out.println("|ff " + (f||f) + " " + (f|f));
  System.out.println("|ft " + (f||t) + " " + (f|t));
  System.out.println("|tf " + (t||f) + " " + (t|f));
  System.out.println("|tt " + (t||t) + " " + (t|t));
}}

As far as I can tell, they are the same:

$ javac A.java && java A
&ff false false
&ft false false
&tf false false
&tt true true
|ff false false
|ft true true
|tf true true
|tt true true
Exception in thread "main" java.lang.NoSuchMethodError: main

Does using || and && improve my code in any way?


As a simple test, I replaced hundreds of occurrences with the short form, and all of my unit tests still pass.

like image 500
Dog Avatar asked Nov 22 '13 19:11

Dog


1 Answers

|| and && uses short circuit evaluation

From same article

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression

Consider you have an object and you want to check one of its property for some value if you do:

if(obj != null & obj.ID == 1)

If your object obj is null you will get a null reference exception, since you used single &, the first condition evaluate to false, but it will still continue to the next condition and raising null reference exception.

If you used && then the second condition will never get evaluated, thus no exception.

if(obj != null && obj.ID == 1)

With your code you will not see any difference, but using bitwise | or & with multiple conditions would result in no short circuiting at all.

More Explanation.


consider that you following code:

Boolean conditionA = 2 > 1; //some condition which returns true
if(conditionA | obj.SomeTimeConsumingMethod()){

}

Now in above code snippet, suppose you have an object with a method SomeTimeConsumingMethod which takes a lot of time in processing and returns true or false. If you use single | it would evaluate both the conditions, since first conditionA is true it will process obj.SomeTimeConsumingMethod as well. End result will be true for the whole if statement since | (OR) is used.

If your condition is using double || (OR Logical operator)

if(conditionA || obj.SomeTimeConsumingMethod())

Then the second condition obj.SomeTimeConsumingMethod() will not be evaluated. This is short circuiting and that just saved you from executing some time consuming method. Still the end result is true regardless of what was returned from obj.SomeTimeConsumingMethod().

like image 125
Habib Avatar answered Sep 23 '22 21:09

Habib