Operators facilitate the execution of operations on variables and values.
In the following example, we apply the + operator to sum two values:
Example
int a = 111 + 77;
Though the + operator is primarily used to sum two values, as shown above, it can also combine a variable and a value, or two variables:
Example
int total1 = 100 + 50; // 150 (100 + 50) int total2 = total1 + 250; // 400 (150 + 250) int total3 = total2 + total2; // 800 (400 + 400)
Java classifies operators into these categories:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Arithmetic operators handle basic mathematical operations.
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Adds two values | x + y |
| – | Subtraction | Subtracts one value from another | x – y |
| * | Multiplication | Multiplies two values | x * y |
| / | Division | Divides one value by another | x / y |
| % | Modulus | Returns the division remainder | x % y |
| ++ | Increment | Increases variable’s value by 1 | ++x |
| — | Decrement | Decreases variable’s value by 1 | –x |
Java Assignment Operators
Assignment operators are utilized to allocate values to variables.
In the subsequent example, we employ the assignment operator (=) to assign the value 10 to a variable named b:
Example
int b = 10;
The addition assignment operator (+=) adds a specified value to a variable:
Example
int b = 10; b += 5;
Here is a comprehensive list of assignment operators:
| Operator | Example | Equivalent To |
|---|---|---|
| = | b = 5 | b = 5 |
| += | b += 3 | b = b + 3 |
| -= | b -= 3 | b = b – 3 |
| *= | b *= 3 | b = b * 3 |
| /= | b /= 3 | b = b / 3 |
| %= | b %= 3 | b = b % 3 |
| &= | b &= 3 | b = b & 3 |
| |= | x |= 3 | x = x | 3 |
| ^= | b ^= 3 | b = b ^ 3 |
| >>= | b >>= 3 | b = b >> 3 |
| <<= | b <<= 3 | b = b << 3 |
Java Comparison Operators
Comparison operators are used to evaluate two values or variables. This is crucial in programming as it helps in determining outcomes and making decisions.
The result of a comparison is a Boolean value, which is either true or false. These concepts will be elaborated in the Booleans and If..Else chapters.
In the following example, we use the greater than (>) operator to ascertain if 5 exceeds 3:
Example
int a = 5; int b = 3; System.out.println(a > b); // returns true, because 5 is greater than 3
| Operator | Name | Example |
|---|---|---|
| == | Equal to | x == y |
| != | Not equal to | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
Java Logical Operators
Logical operators are employed to test for true or false values.
These operators define the logic between variables or values:
| Operator | Name | Description | Example |
|---|---|---|---|
| && | Logical and | Returns true if both expressions are true | x < 5 && x < 10 |
| || | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
| ! | Logical not | Inverts the result, returns false if the result is true | !(x < 5 && x < 10) |


