Java Operators
Operators are the symbols that do something with values: add them, compare them, combine conditions. You have already used + and =. This topic shows the operators you will use every day, grouped by what they do.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | addition | 7 + 3 | 10 |
- | subtraction | 7 - 3 | 4 |
* | multiplication | 7 * 3 | 21 |
/ | division | 7 / 3 | 2 |
% | remainder (modulo) | 7 % 3 | 1 |
public class Main {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
System.out.println(17.0 / 5);
}
}22 12 85 3 2 3.4
Division of two int values drops the remainder, as you saw in Type Casting. The % operator gives that remainder; it is handy to test whether a number is even (n % 2 == 0).
Increment, decrement and compound assignment
x++ adds 1 to x, and x-- subtracts 1. The compound assignment operators change a variable using its own value: x += 5 means x = x + 5, and the same works for -=, *=, /= and %=.
public class Main {
public static void main(String[] args) {
int count = 10;
count++;
System.out.println(count);
count -= 4;
System.out.println(count);
count *= 3;
System.out.println(count);
}
}11 7 21
Comparison operators
Comparisons give a boolean result: true or false. They are the building blocks of decisions in the If ... Else topic.
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> and < | greater than, less than |
>= and <= | greater or equal, less or equal |
Do not confuse = (store a value) with == (compare two values). For text, use equals instead of ==; the Strings topic explains why.
Logical operators
&& (and) is true when both sides are true, || (or) when at least one side is true, and ! (not) turns true into false and false into true:
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasTicket = true;
System.out.println(age >= 18 && hasTicket);
System.out.println(age < 12 || age > 65);
System.out.println(!hasTicket);
}
}true false false
Java stops evaluating && as soon as the left side is false, and || as soon as the left side is true. This is called short-circuit evaluation.
Order of operations
As in mathematics, *, / and % are done before + and -. Use brackets to make the order clear or to change it:
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(0.1 + 0.2);
}
}14 20 0.30000000000000004
The last line shows a surprise: decimal numbers are stored in binary, so 0.1 + 0.2 is not exactly 0.3. For money, programs often count in whole paise or cents with int or long.
Practice exercises
- Print the remainder when 100 is divided by 7.
- Start with
int x = 3;and use compound operators to make it 3, then 9, then 4, printing each step. - Print whether the year 2024 is divisible by 4 and not divisible by 100, using
%,&&and!=.