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);
}
}
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);
}
}
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);
}
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
200
2
Let's have a look at the statements of the method prefixIncrement one by one:
- int a = 0; Declaration and initialization to zero of the variable a.
- ++a; The variable a is incremented by 1.
- System.out.println(a); The value of a is printed to the standard output and that value is 1.
- int b = ++a * 100; The variable b is declared and initialized to the expression ++a * 100. The expression ++a increments a by 1 before using it to evaluate the larger expression ++a * 100. In fact, the statement int b = ++a * 100; is equivalent to these two statements:a = a + 1;It is clear that the variable a is incremented before it is used to evaluate the larger expression.
int b = a * 100; - System.out.println(b); The value of b is printed to the standard output: 200.
- System.out.println(a); The value of a is printed to the standard output: 2.
If you call the method postfixIncrement in the main method of the class App, you will get this output:
1
100
2
100
2
Let's have a look at the statements of the method postfixIncrement one by one:
- int a = 0; Declaration and initialization to zero of the variable a.
- a++; The variable a is incremented by 1.
- System.out.println(a); The value of a is printed to the standard output and that value is 1.
- int b = a++ * 100; The variable b is declared and initialized to the expression a++ * 100. The expression a++ increments a by 1 after having used it to evaluate the larger expression a++ * 100. That is, the statement int b = a++ * 100; is equivalent to these two statements:int b = a * 100;It is clear that the variable a is incremented after it has been used to evaluate the larger expression.
a = a + 1; - System.out.println(b); The value of b is printed to the standard output: 100.
- System.out.println(a); The value of a is printed to the standard output: 2.
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);
}
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
0
Let's have a look at the statements of the method prefixDecrement one by one:
- int a = 1; Declaration and initialization to 1 of the variable a.
- int b = --a * 100; The variable b is declared and initialized to the expression --a * 100. The expression --a decrements a by 1 before using it to evaluate the larger expression --a * 100. In fact, the statement int b = --a * 100; is equivalent to these two statements:a = a - 1;It is clear that the variable a is decremented before it is used to evaluate the larger expression.
int b = a * 100; - System.out.println(b); The value of b is printed to the standard output: 0.
- System.out.println(a); The value of a is printed to the standard output: 0.
If you call the method postfixDecrement in the main method of the class App, you will get this output:
100
0
0
Let's have a look at the statements of the method postfixDecrement one by one:
- int a = 1; Declaration and initialization to 1 of the variable a.
- int b = a-- * 100; The variable b is declared and initialized to the expression a-- * 100. The expression a-- decrements a by 1 after using it to evaluate the larger expression a-- * 100. In fact, the statement int b = a-- * 100; is equivalent to these two statements:int b = a * 100;It is clear that the variable a is decremented after it is used to evaluate the larger expression.
a = a - 1; - System.out.println(b); The value of b is printed to the standard output: 100.
- System.out.println(a); The value of a is printed to the standard output: 0.
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 =:
- op1 += op2; is the same operation as op1 = op1 + op2;
- op1 -= op2; is the same operation as op1 = op1 - op2;
- op1 *= op2; is the same operation as op1 = op1 * op2;
- op1 /= op2; is the same operation as op1 = op1 / op2;
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);
}
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
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);
}
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
Next tutorial : JavaPerspective.com > Beginner Tutorials > 3. Java Basics > 3.6. Java operators examples > 3.6.3. Conditional and relational operators examples