Operators in java code example

Example 1: Operators in java

// example on ternary operator in java
public class TernaryOperatorDemo
{
   public static void main(String[] args)
   {
      int a = 50, b = 500, bigger;
      System.out.println("First number: " + a);
      System.out.println("Second number: " + b);
      bigger = (a > b) ? a : b;
      System.out.println("Bigger number is = " + bigger);
   }
}

Example 2: Operators in java

// Logical operator in java
public class LogicalOperatorDemo
{
   public static void main(String[] args) 
   {
      System.out.println("Logical AND Operator: ");
      int num1 = 20;
      int num2 = 30;
      int num3 = 40;
      boolean output1 = num2 > num1 && num2 < num3;
      System.out.println(output1);
      boolean output2 = num2 < num1 && num2 < num3;
      System.out.println(output1);
      boolean output3 = num2 > num1 && num2 > num3;
      System.out.println(output3);
      boolean output4 = num2 < num1 && num2 > num3;
      System.out.println(output4);
      System.out.println("Logical OR Operator: ");
      output1 = num2 > num1 || num2 < num3;
      System.out.println(output1);
      output2 = num2 < num1 || num2 < num3;
      System.out.println(output2);
      output3 = num2 > num1 || num2 > num3;
      System.out.println(output3);
      output4 = num2 < num1 || num2 > num3;
      System.out.println(output4);
      System.out.println("Logical NOT Operator: ");
      boolean temp = false ;
      boolean temp1 = !temp;
      System.out.println("Temp: " + temp);
      System.out.println("Logical not: " + temp1);
      int n1 = 30;
      int n2 = 45;
      int n3 = 50;
      boolean result1 = n1 < n2;
      boolean result2 = !(n1 < n2);
      System.out.println(result1);
      System.out.println(result2);
      boolean result3 = n1 < n2 && n2 < n3;
      boolean result4 = !(n1 < n2 && n2 < n3);
      System.out.println(result3);
      System.out.println(result4);
   }
}

Example 3: Operators in java

// java assignment operator with examples
public class AssignmentOperatorDemo
{
   public static void main(String[] args)
   {
      int numOne = 60;
      int numTwo = 30;
      numTwo += numOne;
      System.out.println("(+=) : " + numTwo);
      numTwo -= numOne;
      System.out.println("(-=) : " + numTwo);
      numTwo *= numOne;
      System.out.println("(*=) : " + numTwo);
      numTwo /= numOne;
      System.out.println("(/=) : " + numTwo);
      numTwo %= numOne;
      System.out.println("(%=) : " + numTwo);
   }
}

Example 4: Operators in java

// Arithmetic operators in java
public class ArithmeticOperatorDemo
{
   public static void main(String[] args)
   {
      int i = 25;
      int j = 5;
      System.out.println(i + j);
      System.out.println(i - j);
      System.out.println(i * j);
      System.out.println(i / j);
      System.out.println(i % j);
   }
}

Example 5: 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 6: Operators in java

// java unary (~) operator
public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 8, b = -4;
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println(a + " java bitwise complement is = " + ~a);
      System.out.println(b + " java bitwise complement ia = " + ~b);
   }
}

Example 7: 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));
   }
}

Example 8: Operators in java

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

Tags:

Java Example