logical operators in java with example

Example 1: condition operator in java

b = (a == 1) ? 20: 30;

Example 2: Operators in java

// java unary increment (++) operator
public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 23;
      // 23 gets printed and increment to 24
      System.out.println("Java post-increment = " + a++);
      // a value was 24, incremented to 25
      System.out.println("Java pre-increment = " + ++a);
   }
}

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