Summary: in this tutorial, you’ll learn how to use Java arithmetic operators to perform various mathematical operations on numeric values.
Introduction to Java arithmetic operators
Arithmetic operators allow you to perform mathematical operations on numeric values. Java provides all basic arithmetic operators that perform addition (+), subtraction (-), multiplication (+), and division (/). Also, it offers a remainder operator (%) that returns the remainder of a division.
The following table illustrates the arithmetic operators in Java:
Operator | Description |
---|---|
+ | Additive operator |
- | Subtraction operator |
* | Multiplication operator |
/ | Division operator |
% | Remainder operator |
These arithmetic operators can be applied to both integers and floats. Depending on the type of numbers, the result will be different. For example:
Integer | Float | ||||
Operation | Operator | Equation | Result | Equation | Result |
Add | + | 15 + 2 | 17 | 15.0 + 2.0 | 17.0 |
Subtract | – | 15 – 2 | 13 | 15.0 – 2.0 | 13.0 |
Multiply | * | 15 * 2 | 30 | 15.0 * 2.0 | 30.0 |
Divide | / | 15/2 | 7 | 15.0/2.0 | 7.5 |
Remainder | % | 15%2 | 1 | 15.0%2.0 | 1.0 |
It’s important to note that when you divide two integers, you’ll get an integer, not a float. Therefore, there will be no fractional part. e.g., 15/2 returns 7 instead of 7.5.
The following program illustrates how to use the Java arithmetic operators on integers:
public class App {
public static void main(String[] args) {
int x = 15;
int y = 2;
int result = x + y;
System.out.println(result); // 17
result = x - y;
System.out.println(result); // 13
result = x * y;
System.out.println(result); // 30
result = x / y;
System.out.println(result); // 7
result = x % y;
System.out.println(result); // 1
}
}
Code language: Java (java)
Output:
17
13
30
7
1
Code language: Java (java)
Similarly, the following program shows how to use the Java arithmetic operators on floating-point numbers:
public class App {
public static void main(String[] args) {
float x = 15.0f;
float y = 2.0f;
float result = x + y;
System.out.println(result); // 17.0
result = x - y;
System.out.println(result); // 13.0
result = x * y;
System.out.println(result); // 30.0
result = x / y;
System.out.println(result); // 7.5
result = x % y;
System.out.println(result); // 1.0
}
}
Code language: Java (java)
Output:
17.0
13.0
30.0
7.5
1.0
Code language: Java (java)
Prefix and postfix operators
The prefix and postfix operators are used with variables to increment and decrement their values by one.
Prefix operators
A prefix operator is placed before a variable. Java provides two prefix operators ++x
and --x
, where x
is a variable.
The ++x
prefix operator increments a variable’s value by one and the --x
prefix operator decrements a variable value by one before the value is used in an expression.
The following example illustrates how to use the ++count
prefix operator to increment the count
variable by one:
public class App {
public static void main(String[] args) {
int count = 0;
System.out.println(++count); // 1
System.out.println(count); // 1
}
}
Code language: Java (java)
Output:
1
1
Code language: Java (java)
How it works.
- First, define a variable called
count
and initialize its value to 0. - Second, increment the
count
variable by one and return it. TheSystem.out.println()
displays the value of thecount
variable which is 1. - Third, display the value of the
count
variable again. As a result, it shows 1 as expected.
The following program shows how to use the prefix operator --count
to decrease the value of the count
variable by one:
public class App {
public static void main(String[] args) {
int count = 1;
System.out.println(--count); // 0
System.out.println(count); // 0
}
}
Code language: Java (java)
Output:
0
0
Code language: Java (java)
Postfix operators
The postfix operators are placed after a variable. Java has two postfix operators: x++
and x--
, where x
is a variable.
The postfix operator x++
increments the variable’s value and x--
decrements the variable’s value after the value is used in the expression.
The following program uses the postfix operator count++
to increment a variable’s value after the value is used in the System.out.println()
:
public class App {
public static void main(String[] args) {
int count = 0;
System.out.println(count++); // 0
System.out.println(count); // 1
}
}
Code language: Java (java)
Output:
0
1
Code language: Java (java)
The count++
returns the value of the count
variable first before incrementing the count
‘s value by one. Therefore, the first System.out.println()
displays 0 while the second one shows 1.
The following program illustrates how to use the postfix operator count--
to decrement a variable:
public class App {
public static void main(String[] args) {
int count = 1;
System.out.println(count--); // 1
System.out.println(count); // 0
}
}
Code language: Java (java)
Output:
1
0
Code language: Java (java)
In this example, we define the count
variable and initialize its value to 1. The count--
returns the count’s value first and then decreases the count
‘s value by one.
Compound assignment operators
To add a value to a variable, you use the +
operator and assign the result back to the variable. For example, the following adds 2
to the x
variable:
int x = 15;
x = x + 2;
Code language: Java (java)
Since the variable x
appears on both the left and right sides of the expression, it’s quite redundant.
To remove the redundancy, Java allows you to use something called a compound assign operator like this:
int x = 15;
x += 2;
Code language: Java (java)
In this example, the x += 2
is equivalent to the x = x + 2
.
By definition, a compound assignment operator combines an operator (addition, subtraction, multiplication, division, modular) with the assignment operator (=):
Arithmetic Operators | Compound Assignment Operators |
---|---|
x = x + n; | x += n; |
x = x – n; | x -= n; |
x = x * n; | x *= n; |
x = x / n; | x /= n; |
x = x % n; | x %= n; |
Operator precedence
If an expression contains multiple operators, Java uses a set of rules that determines the order in which operators are evaluated.
This set of rules is called operator precedence. In Java, the operator that has higher precedence will be evaluated first.
The following table shows the precedence of postfix operators, prefix operators, and arithmetic operators from high to low:
Operator | Operator Precedence (high to low) |
---|---|
Postfix operators | x++, x– |
Prefix operators | ++x, –x |
Multiplicative operators | *, / , % |
Additive operators | +, – |
The operators with the same precedence e.g., (+, -) are evaluated from left to right. To override precedence, you use parenthesis. If nested parenthesis is used, they are evaluated from the inside out.
The following program illustrates how operator precedence works:
public class App {
public static void main(String[] args) {
int x = 1;
int y = 2;
int z = 3;
int result1 = x + y * z;
int result2 = (x + y) * z;
System.out.println(result1); // 7
System.out.println(result2); // 9
}
}
Code language: Java (java)
In this program, we have three variables x
, y
, and z
and perform two different calculations:
result1
calculatesx + y * z
. Since multiplication has higher precedence than addition,y * z
is evaluated first, and then the result is added tox
. Theresult1
is7
because multiplication is performed before addition:1 + 2 * 3
equals1 + 6
, which is7
.result2
calculates(x + y) * z
. Here, we use parentheses to explicitly specify that the addition should be performed first, and then the result is multiplied byz
. Theresult2
is9
because we use parentheses to ensure addition is performed first:(1 + 2) * 3
equals3 * 3
, which is9
.
Summary
- Java supports basic arithmetic operators including +, -, *, /, and %.
- Use the prefix and postfix operators to increment or decrement a variable by one.
- Use compound assignment operators to make the code more concise.