Saturday 31 October 2015

Difference between logical and bitwise operators in Java(&&,|| and &,|)

       The logical(non-bitwise) operators && and || are short-circuit operators. In other words, with && if the LHS(Left Hand Side) is false, the RHS(Right Hand Side) will never be evaluated. With || if the LHS is true, then the RHS will never be evaluated.  On the other hand, the bitwise operators & and | are non short-circuit, will always evaluate both the LHS and RHS.
     This is especially useful in guarding against nullness.

      String x = null;

            if ( x != null && x.equals("xyz") {

                   then do something with x...

            }
                
      The above condition is the best example for handle the null exception. If u put first condition is not equal to null then only it will check for other conditions.
 

      String x = null;

            if ( x != null & x.equals("xyz") {

                   then do something with x...

            }

      In the above example (bitwise & operator) ,it will check the all conditions in the if statements, so it will throw the NullPointerException.
       One main difference between & and && is, bitwise &  you can use with both integral and boolean but && you can use with only boolean operands.

Some Programs based on logical & bitwise operators:--

1) What is the output of given code?

 class OperatorExOne {
      public static void main (String[] args) {
             String a = null;
             if(a != null && a.equals("Java")) {
                   System.out.println("Inside If Statement");
             } else {
                    System.out.println("Inside else Statement");
             }
      }
 }

 Output:- Inside else Statement
       
       In the above code it will print else part i.e "Inside else Statement". In if statement there are two conditions with && operator. In logical AND operator it will check the first condition if it is false then it won't check the 2nd condition and it will return false. In the above code first condition is false so it won't check the 2nd condition. In second condition it is exception but it won't check the condition at all.
      In the above code,if we changed the first condition of if statement, then it will throw NullPointerException, See below.

2) What is the output of given code?


 class OperatorExTwo {
                     
       public static void main (String[] args) {
              
              String a = null;
                            
              if(a.equals("a") && a != null ) {
                     System.out.println ("Inside If Statement");
              } else {
                     System.out.println ("Inside else Statement");
              }
       }
 }  


Output:-java.lang.NullPointerException


         Above code will throw  NullPointerException because the first condition of if statement is true and it will check the second condition, and will throw NullPointerException.


3) What is the output of given code?


 class OperatorExThree {
       
       public static void main (String[] args) {
             
             int a = 2;
             int b = 3;
             int result = a & b;
             System.out.println(result);
      }
 }

Output:- 2


4) What is the output of given code?

 class OperatorExFour {
       
       public static void main (String[] args) {
             
             int a = 2;
             int b = 3;
             int result = a && b;       //Compilation error
             System.out.println(result);
      }
 }

 Output:-  Compilation error,

The operator && is undefined for the argument
                 type(s) int, int
 


Related Posts:--
1) String Interview Questions and Answers
2) Exception Handling Interview questions and answers
3) Interface and Abstract Class Interview Questions and Answers

1 comment:

  1. Hi Anil,

    Amaze! I have been looking Bing for hours because of this and I also in the end think it is in this article! Maybe I recommend you something helps me all the time?
    I'm developing a rcp plugin application and implement a log4j to log in but when I export the plugin as a product of the eclipse, the log file doesn't generate.
    Reduce number of Layers. In a multi-tier environment, we should re-evaluate the need of each layer. Sometime architects tend to introduce a layer for the sake of future extensibility, but it might significantly reduce performance.
    Where should I put the log4j.properties in my application of the plugin rcp?
    Follow my new blog if you interested in just tag along me in any social media platforms!

    Cheers,
    Chandu

    ReplyDelete