Java Operators
Operators in Java are symbols that perform actions on values and variables. They let you calculate numbers, store values, compare results, and combine true-or-false conditions.
You have already seen the assignment operator =, which stores a value in a variable. This lesson covers the operators beginners use most often in Java expressions.
Arithmetic Operators
Arithmetic operators work with numbers. They can add, subtract, multiply, divide, and find a remainder.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Remainder | a % b |
The remainder operator % gives what is left after division. For example, 17 % 5 gives 2 because 5 fits into 17 three times with 2 left over.
Example
public class Main {
public static void main(String[] args) {
int a = 17;
int b = 5;
System.out.println("Sum: " + (a + b));
System.out.println("Difference: " + (a - b));
System.out.println("Product: " + (a * b));
System.out.println("Integer division: " + (a / b));
System.out.println("Remainder: " + (a % b));
}
}
Output:
Sum: 22
Difference: 12
Product: 85
Integer division: 3
Remainder: 2
How It Works
The variables a and b are both int values. Because both sides of a / b are integers, Java performs integer division and removes the decimal part. The result is 3, not 3.4.
If you need a decimal result, use a decimal type such as double. For example, dividing 17.0 by 5.0 would produce 3.4.
Assignment Operators
The assignment operator = stores a value in a variable. Java also has compound assignment operators that update a variable using its current value.
| Operator | Example | Same As |
|---|---|---|
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
These operators are common when a value changes step by step, such as adding points to a score or reducing a count.
Comparison Operators
Comparison operators test two values and produce a boolean result: true or false.
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Use == when comparing values. A single = assigns a value instead of checking equality.
Logical Operators
Logical operators combine or change boolean expressions. && means both conditions must be true, || means at least one condition must be true, and ! means not.
public class Main {
public static void main(String[] args) {
int score = 85;
int attempts = 2;
boolean passed = score >= 70;
boolean canRetry = attempts < 3;
boolean needsHelp = score < 60 || attempts >= 3;
score += 5;
System.out.println("Passed: " + passed);
System.out.println("Can retry: " + canRetry);
System.out.println("Needs help: " + needsHelp);
System.out.println("Updated score: " + score);
}
}
Output:
Passed: true
Can retry: true
Needs help: false
Updated score: 90
The expression score >= 70 is true because 85 is greater than or equal to 70. The expression score < 60 || attempts >= 3 is false because neither condition is true.
Increment And Decrement
Java also has shortcuts for adding or subtracting exactly 1. The operator ++ increases a variable by one, and -- decreases a variable by one.
public class Main {
public static void main(String[] args) {
int lives = 3;
lives--;
System.out.println("After losing one: " + lives);
lives++;
System.out.println("After gaining one: " + lives);
}
}
Output:
After losing one: 2
After gaining one: 3
Common Mistakes
- Using
=when you meant==in a comparison. - Expecting integer division to keep the decimal part.
- Using
%when you need normal division instead of the remainder. - Forgetting that
&&requires both conditions to be true.
Takeaway: operators are the building blocks of Java expressions, and the type of each value affects the result you get.
