Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.2. Arithmetic operators examples

3.6.2. Arithmetic operators examples
Last updated: 27 January 2013.


3.6.2.1. Operator + example

This tutorial builds on the previous one. In particular, you can reuse the class App from the previous tutorial to run the examples provided in this tutorial.

Let's add another method named add to the class JavaOperators to illustrate the use of the arithmetic operator +:

public class JavaOperators {

   
public void add(){

         
int op1 = 15;
         
int op2 = 3;

          System.out.println
(op1 + op2);
   
}

}


The output is:

18



3.6.2.2. Operators - , * , / and % examples

Likewise, you can add the methods subtract, multiply, divide and remainder to test the operators - , * , / and % respectively.

public class JavaOperators {

   
public void divide(){

         
int op1 = 15;
         
int op2 = 3;

          System.out.println
(op1 / op2);
   
}

   
public void multiply(){

         
int op1 = 15;
         
int op2 = 3;

          System.out.println
(op1 * op2);
   
}

   
public void remainder(){

         
int op1 = 15;
         
int op2 = 3;

          System.out.println
(op1 % op2);
   
}

   
public void subtract(){

         
int op1 = 15;
         
int op2 = 3;

          System.out.println
(op1 - op2);
   
}

}



3.6.2.3. Operator ++ examples

Now let's add to the class JavaOperators two methods: prefixIncrement and postfixIncrement to illustrate the two modes of the operator ++:

public void prefixIncrement() {

   
int a = 0;

    ++a;

    System.out.println
(a);

   
int b = ++a * 100;

    System.out.println
(b);

    System.out.println
(a);

}


public void postfixIncrement() {

   
int a = 0;

    a++;

    System.out.println
(a);

   
int b = a++ * 100;

    System.out.println
(b);

    System.out.println
(a);

}


If you call the method prefixIncrement in the main method of the class App, you will get this output:

1
200
2

Let's have a look at the statements of the method prefixIncrement one by one:


If you call the method postfixIncrement in the main method of the class App, you will get this output:

1
100
2

Let's have a look at the statements of the method postfixIncrement one by one:


3.6.2.4. Operator -- examples

Let's add to the class JavaOperators two methods: prefixDecrement and postfixDecrement to illustrate the two modes of the operator --:

public void prefixDecrement() {

   
int a = 1;

   
int b = --a * 100;

    System.out.println
(b);

    System.out.println
(a);

}


public void postfixDecrement() {

   
int a = 1;

   
int b = a-- * 100;

    System.out.println
(b);

    System.out.println
(a);

}


If you call the method prefixDecrement in the main method of the class App, you will get this output:

0
0

Let's have a look at the statements of the method prefixDecrement one by one:


If you call the method postfixDecrement in the main method of the class App, you will get this output:

100
0

Let's have a look at the statements of the method postfixDecrement one by one:


3.6.2.5. Operators += , -= , *= and /= examples

Actually there are 4 additional arithmetic operators that are combinations of the simple operators +, -, *, / and the assignment operator =:
public void combinedOperatorsTest(){

   
int op1 = 1;
   
int op2 = 2;
   
int op3 = 3;
   
int op4 = 4;

    op1 +=
1;

    op2 -=
1;

    op3 *=
2;

    op4 /=
2;

    System.out.println
(op1);
    System.out.println
(op2);
    System.out.println
(op3);
    System.out.println
(op4);
}

The output is:

2
1
6
2



3.6.2.6. Operator ! example

Now let's add to the class JavaOperators a method called invert in order to illustrate the use of the operator !:

public void invert(){

   
boolean bool = true;

    System.out.println
(! bool);
}

The output is:

false


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.2. Arithmetic operators examples
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   3. Java Basics  >   3.6. Java operators examples  >   3.6.3. Conditional and relational operators examples

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7