Operators and Assignments Part-3

Operators in Java

Operators can be categorized in the  following types:

  • Bit-wise Operator

    Bit-wise operators are (and), | (or), ^ (x or), ! (complement) and (tiled). Studying these operators separately as:

    • & operator:

      This operator gives the boolean value true only when all the conditions are true. This operator can be used on more than 2 conditions simultaneously. For example:

      int i=10,j=20;
      System.out.println(i==10 & j==20);               //output will be true
      System.out.println(i==20 & j==20);               //output will be false
      System.out.println(i==10 & j==20 & 10==20);      //output will be true

      Note- In the above code 1st line’s output is true because both the conditions are true. But in the 2nd line, output is false because one of the condition is false.

    • | operator: This operator gives the boolean value true when any one of the conditions are true. For example:
      int i=10,j=20;
      System.out.println(i==10 | j==20);               //output will be true
      System.out.println(i==20 | j==20);               //output will be true

      Note- In the above code 1st line’s output is true because both the conditions are true. And the 2nd line’s output is true because at least one of the condition is true.

    • ^ operator:

      This operator gives the boolean value true when the results of all the conditions are different. If there are more than 2 conditions then the operation is applied on 2 of the conditions at a time. For example:

      int i=10,j=20;
      System.out.println(i==10 ^ j==20);                       //output will be false
      System.out.println(i==20 ^ j==20);                       //output will be true
      System.out.println(i==20 ^ j==20 ^ 10==20 ^ 20==20);     //output will be false
      System.out.println(i==10 ^ j==20 ^ 10==20 ^ 20==20);     //output will be true

      Note- In the above code 1st line’s output is false because both the conditions are true. And the 2nd line’s output is true because one of the condition is true and other is false. Interestingly in 3rd line, there are 2 true and 2 false conditions, so the output is false. But in 4th line, there are 3 true and 1 false condition, hence it is true.

    • ~ operator:

      It is known as tiled operator. It is a unary operator which when applied to a input value returns its compliment representation. Basically it reverses all the bits and returns them. It is applicable for only integral data types and not boolean data types. For example,

      System.out.println(~1);
      //output will be -2
    • ! operator:

      It is a complimentary operator applicable to only boolean data types. When it is given any boolean value, it returns its reverse value. For example,

      System.out.println(!true);
      //output will be false
      System.out.println(!false);
      //output will be true
      System.out.println(!2);
      //invalid

  • Short-Circuit

    Short circuit operators ( &&, || ) are very similar to bit-wise operator. In comparison with bit-wise operator these operators have relatively high performance and is applicable for only boolean data types. It has high performance because the second condition is checked when the previous conditions are true. This kind of evaluation technique saves the JVM time. Some of the example of short circuit operator are:

    System.out.println(10==10 && 20>15);
    //output will be true
    System.out.println(10<10 && 20==20);
    //output will be false

    Note- As in the first case, first condition is true so the JVM will go to check second condition. But in the second case first condition is false and therefore second condition will not be check by the JVM.

Leave a Comment

Your email address will not be published. Required fields are marked *