AND OR operator java code example

Example 1: java or

int num = 9;

if (num == 1 || num == 2) {
  System.out.println("Nope");
  }

if (num == 1 || num == 9) {
  System.out.println("Yep");
  }

Example 2: |= java operator

foo = 32;   // 32 =      0b00100000
bar = 9;    //  9 =      0b00001001
baz = 10;   // 10 =      0b00001010
foo |= bar; // 32 | 9  = 0b00101001 = 41
            // now foo = 41
foo |= baz; // 41 | 10 = 0b00101011 = 43
            // now foo = 43

Example 3: Operators in java

// example on java unary NOT (!) operator
public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      boolean bool = true;
      int a = 14, b = 5;
      System.out.println("Before using java unary ! operator: " + bool);
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println("After using java unary ! operator: " + !bool);
      System.out.println("!(a < b) = " + !(a < b));
      System.out.println("!(a > b) = " + !(a > b));
   }
}

Tags:

Java Example