·  JAVA – Syntax – Operators – Arithmetic

 

- Info:

  - This tutorial shows how to use Arithmetic Operators in JAVA.: - + - * / % ++ -- !.

  - If one of the operands is float or double the other one is implicitly transformed into that type.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

      

      public static void main(String[] args) {

   

        int     x          = 10;

        int     y          = 20;

      

        int     negate     =  -y;        //-10       = -10

        int     add        = x+y;        //10+20     =  30

        int     subtract   = x-y;        //10-20     = -10

        int     multiply   = x*y;        //10*20     =  200

        int     divide1    = x/y;        //10/20     =  0.5     = 0

        float   divide2    = (float)x/y; //10.0/20.0 =  0.5 

        int     reminder   = x%y;        //10%20     =  0*20+10 = 10

        int     increment1 = ++x;        //Increment x by 1 and then store x into increment1.

        int     decrement1 = --x;        //Decrement x by 1 and then store x into decrement1.

        int     increment2 =   x++;      //Store x into increment2 and then increment x by 1.

        int     decrement2 =   x--;      //Store x into decrement2 and then decrement x by 1.

        boolean invert     = !true;      //Inverts value of boolean.

        

        System.out.println(negate    );

        System.out.println(add       );

        System.out.println(subtract  );

        System.out.println(multiply  );

        System.out.println(divide1   );

        System.out.println(divide2   );

        System.out.println(reminder  );

        System.out.println(increment1);

        System.out.println(decrement1);

        System.out.println(increment2);

        System.out.println(decrement2);

        System.out.println(invert    );

   

      }

    }