Learn Java Arithmetic Operators and Compound Assignment with Examples
1. Basic Arithmetic Operators in Java
The following table lists the basic arithmetic operators (or operations) used in Java:
Arithmetic Operators
| Operation | Description |
|---|---|
| + | Addition (also unary plus) |
| - | Subtraction (also unary minus) |
| * | Multiplication |
| / | Division |
| % | Modulus (remainder of division) |
Here are some rules for working with arithmetic operators:
- Expressions are evaluated from left to right unless parentheses are used or some operations have higher precedence.
- The
*,/, and%operations have higher precedence than+and-.
Arithmetic Operators with Integer Values
For example, in this code, variables a and b will have different values:
public class BasicIntMath {
public static void main(String[] args) {
int a = 4 + 5 - 2 * 3;
int b = 4 + (5 - 2) * 3;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
} Output:
a = 3
b = 13 Unary Addition and Subtraction Operations
- The unary subtraction operation changes the sign of its single operand.
- The unary addition operation simply returns the value of its operand. It is not necessary but is allowed.
public class UnarySignOperation {
public static void main(String[] args) {
double a = -6;
double b = +6;
System.out.println(a);
System.out.println(b);
}
} Integer Division in Java
When division is performed on integer types, the result does not contain a fractional component. This is integer division in Java:
public class IntDivision {
public static void main(String[] args) {
int a = 16 / 5;
System.out.println(a);
}
} Output:
3 Arithmetic Operators with char Type Variables
Arithmetic operators require numeric types. They cannot be used with boolean data types, but they are allowed for char types, as char is essentially a subtype of int in Java. For example:
public class BasicCharMath1 {
public static void main(String[] args) {
char c = 'n';
System.out.println(c);
System.out.println(c + 1);
System.out.println(c / 5);
}
} Output:
n
111
22 Or in the following example:
public class BasicCharMath2 {
public static void main(String[] args) {
char c1 = '1';
char c2 = '\u0031';
char c3 = 49;
System.out.println(c1 + c2 + c3);
}
} Output:
147 Modulus Division in Java
The modulus operator is represented by %. It returns the remainder of division in Java. For integer division, the result is also an integer:
public class DivisionByModule {
public static void main(String[] args) {
int a = 6 % 5;
double b = 6.2 % 5.0;
System.out.println(a);
System.out.println(b);
}
} Output:
1
1.2000000000000002 2. Compound Arithmetic Operations with Assignment
Compound Assignment Operators
| Operation | Description |
|---|---|
| += | Addition with assignment |
| -= | Subtraction with assignment |
| *= | Multiplication with assignment |
| /= | Division with assignment |
| %= | Modulus with assignment |
Java provides special operations that combine arithmetic operators with assignment. Consider the following expression:
a = a + 4; In Java, this operation can be written as:
a += 4; Compound assignment operations not only reduce code length but also perform automatic type conversion, which regular operations do not:
public class CompoundOperations {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
a += 3;
b *= 2;
c += a * b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
} The example above uses only int variables, so the implicit conversion isn't visible. It becomes clear with a smaller type. Consider a byte variable: the compound operator += compiles, while the equivalent full form does not:
public class CompoundConversion {
public static void main(String[] args) {
byte b = 10;
b += 5; // OK: += implicitly casts the result back to byte
// b = b + 5; // Compilation error: b + 5 is an int and can't be assigned to a byte
System.out.println(b); // 15
}
} Here b += 5 works because the compound operator automatically casts the int result of the addition back to byte. Writing the same as b = b + 5 would cause a compilation error, since an int can't be assigned to a byte without an explicit cast. This is the "automatic type conversion" that regular operators don't provide.
Frequently Asked Questions
Why does 16 / 5 give 3 in Java instead of 3.2?
Because both operands are integers (int), and dividing integer values produces an integer result: the fractional part is simply discarded, with no rounding. So 16 / 5 gives 3. To get the decimal result 3.2, at least one operand must be a floating-point number, for example 16.0 / 5 or 16 / 5.0.
What does the modulo operator % do?
The % operator returns the remainder of dividing the first number by the second. For example, 6 % 5 is 1, because 6 divides by 5 once with a remainder of 1. It also works with floating-point numbers: 6.2 % 5.0 gives 1.2. Modulo is often used to check whether a number is even (n % 2 == 0) or to get a remainder in cyclic calculations.
Why can arithmetic operators be applied to the char type?
Because in Java the char type is essentially a kind of integer type: it stores a character's Unicode code. When used in arithmetic, a char is automatically converted to its numeric code. For example, the character 'n' has the code 110, so 'n' + 1 gives 111. Arithmetic cannot be applied only to the boolean type.
How does a += 4 differ from a = a + 4?
The computed result is the same, but the compound operator += also performs an implicit cast of the result back to the variable's type. So for byte b = 10;, the statement b += 5; compiles, while b = b + 5; does not: in the second case the result of b + 5 is an int, which can't be assigned to a byte without an explicit cast. In addition, += makes the code shorter.
Comments