
Variables store data. Operators do things with it. They add numbers, compare values, combine conditions, and assign results. Java has dozens of operators, but you’ll use the same handful constantly.
Arithmetic Operators
These work like the math you already know.
int a = 10;
int b = 3;
System.out.println(a + b); // 13 (addition)
System.out.println(a - b); // 7 (subtraction)
System.out.println(a * b); // 30 (multiplication)
System.out.println(a / b); // 3 (division)
System.out.println(a % b); // 1 (modulus - remainder)
The modulus operator (%) returns the remainder after division. 10 divided by 3 is 3 with a remainder of 1. Modulus gives you that 1.
Modulus is surprisingly useful. Need to check if a number is even? Divide by 2 and check if the remainder is 0.
int number = 7;
if (number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Integer Division
When you divide two integers, Java discards the decimal part. It doesn’t round. It truncates.
int result = 7 / 2;
System.out.println(result); // 3, not 3.5
If you need the decimal, at least one operand must be a floating-point type:
double result = 7.0 / 2;
System.out.println(result); // 3.5
// Or cast one operand
int x = 7;
int y = 2;
double result2 = (double) x / y;
System.out.println(result2); // 3.5
Assignment Operators
The basic assignment operator is =. It puts the value on the right into the variable on the left.
int score = 100;
Compound assignment operators combine arithmetic with assignment. They’re shortcuts.
int score = 100;
score += 10; // same as: score = score + 10; (now 110)
score -= 5; // same as: score = score - 5; (now 105)
score *= 2; // same as: score = score * 2; (now 210)
score /= 3; // same as: score = score / 3; (now 70)
score %= 15; // same as: score = score % 15; (now 10)
These aren’t just shorter to type. They signal intent more clearly. When you see score += 10, you immediately know you’re adding to an existing value.
Increment and Decrement
Adding or subtracting 1 is so common that Java has dedicated operators for it.
int count = 5;
count++; // count is now 6
count--; // count is now 5 again
You can place the operator before or after the variable. The difference matters when you use the expression’s value.
int a = 5;
int b = a++; // b gets 5, then a becomes 6
int c = 5;
int d = ++c; // c becomes 6, then d gets 6
With a++ (postfix), the original value is used first, then the increment happens. With ++c (prefix), the increment happens first, then the new value is used.
In practice, most developers use these operators in simple statements where the distinction doesn’t matter:
for (int i = 0; i < 10; i++) {
// i++ here - the return value isn't used
}
Comparison Operators
Comparison operators compare two values and return a boolean: true or false.
int x = 10;
int y = 5;
System.out.println(x == y); // false (equal to)
System.out.println(x != y); // true (not equal to)
System.out.println(x > y); // true (greater than)
System.out.println(x < y); // false (less than)
System.out.println(x >= y); // true (greater than or equal)
System.out.println(x <= y); // false (less than or equal)
Note the double equals == for comparison. Single equals = is assignment. Mixing them up is a common beginner mistake.
int score = 100;
// Wrong - this assigns 50 to score
if (score = 50) { // compiler error in Java
}
// Right - this compares score to 50
if (score == 50) {
}
Java actually catches this mistake at compile time for boolean conditions. Some languages don’t.
Comparing Strings
For objects like Strings, == checks if two variables point to the same object in memory. Usually you want to compare the actual text content. Use .equals() instead.
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same content)
This trips up almost every Java beginner. Use == for primitives. Use .equals() for objects.
Logical Operators
Logical operators combine boolean values. They’re essential for complex conditions.
&& (AND) returns true only if both sides are true.
boolean hasTicket = true;
boolean hasID = true;
if (hasTicket && hasID) {
System.out.println("You may enter");
}
|| (OR) returns true if at least one side is true.
boolean isWeekend = false;
boolean isHoliday = true;
if (isWeekend || isHoliday) {
System.out.println("Day off!");
}
! (NOT) flips a boolean value.
boolean isLoggedIn = false;
if (!isLoggedIn) {
System.out.println("Please log in");
}
Short-Circuit Evaluation
Java evaluates logical expressions left to right and stops as soon as it knows the answer.
With &&, if the left side is false, the right side never runs. The result is already false regardless.
With ||, if the left side is true, the right side never runs. The result is already true regardless.
String name = null;
// Safe - the second condition never runs if name is null
if (name != null && name.length() > 0) {
System.out.println("Name: " + name);
}
Without short-circuit evaluation, name.length() would crash when name is null. This pattern is useful for guarding against null values.
Combining Operators
Real code combines multiple operators. Here’s a program that checks if someone qualifies for a discount:
public class DiscountCheck {
public static void main(String[] args) {
int age = 25;
boolean isMember = true;
double purchaseAmount = 75.00;
// Seniors (65+) always get a discount
// Members get a discount on purchases over $50
// Everyone gets a discount on purchases over $100
boolean qualifies = (age >= 65) ||
(isMember && purchaseAmount > 50) ||
(purchaseAmount > 100);
if (qualifies) {
double discount = purchaseAmount * 0.10;
double finalPrice = purchaseAmount - discount;
System.out.println("Discount applied!");
System.out.println("You save: $" + discount);
System.out.println("Final price: $" + finalPrice);
} else {
System.out.println("No discount available");
System.out.println("Total: $" + purchaseAmount);
}
}
}
Output:
Discount applied!
You save: $7.5
Final price: $67.5
Operator Precedence
When an expression has multiple operators, Java follows precedence rules to decide which operations happen first.
int result = 2 + 3 * 4; // result is 14, not 20
Multiplication happens before addition, just like in math class.
Here’s the precedence order for operators covered in this article, from highest to lowest:
++,--,!(unary operators)*,/,%+,-<,>,<=,>===,!=&&||=,+=,-=, etc.
When in doubt, use parentheses. They make your intent clear and prevent bugs.
// Confusing
boolean result = a > b && c < d || e == f;
// Clear
boolean result = ((a > b) && (c < d)) || (e == f);
Parentheses cost nothing and help everyone reading your code, including future you.
The Ternary Operator
The ternary operator is a compact if-else in a single expression.
int score = 75;
String result = (score >= 60) ? "Pass" : "Fail";
System.out.println(result); // Pass
The syntax is: condition ? valueIfTrue : valueIfFalse
It’s useful for simple assignments:
int a = 10;
int b = 20;
int max = (a > b) ? a : b; // max is 20
String status = (count == 1) ? "item" : "items";
System.out.println(count + " " + status);
Don’t nest ternary operators. It becomes unreadable fast. Use a regular if-else instead.
// Don't do this
String grade = (score >= 90) ? "A" : (score >= 80) ? "B" : (score >= 70) ? "C" : "F";
// Do this instead
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "F";
}
Common Mistakes
Using = instead of == in conditions. Assignment vs comparison. Java usually catches this, but stay alert.
Forgetting integer division truncates. If you expect decimals, use double or cast.
Comparing Strings with ==. Use .equals() for String content comparison.
Confusing && and ||. AND requires both conditions true. OR requires at least one. Think carefully about which you need.
Ignoring operator precedence. When combining operators, use parentheses to make grouping explicit.
What’s Next
You now have variables to store data and operators to manipulate it. The next step is controlling program flow with conditionals and loops, which let your code make decisions and repeat actions.
Related: Java Variables and Data Types | Working with Strings in Java
Sources
- Oracle. “Operators.” The Java Tutorials. docs.oracle.com
- Oracle. “Equality, Relational, and Conditional Operators.” The Java Tutorials. docs.oracle.com


