java in operator code example

Example 1: 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 2: 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 3: 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