Operators
An operator is a symbol that performs an operation on one or more values.
+ adds two numbers. > compares them. && combines conditions. Operators are how your program does work — every calculation, every decision, every condition relies on them.
Java has six categories of operators. This page covers all of them with examples you can run immediately.
👉 What you'll learn here:
- ›Arithmetic operators — math operations
- ›Assignment operators — storing results efficiently
- ›Comparison operators — comparing values
- ›Logical operators — combining conditions
- ›Unary operators — single-value operations
- ›Ternary operator — one-line if/else
- ›Operator precedence — which runs first
🧘 You don't need to memorize every operator right now.
Read through once to understand what exists. You'll naturally remember the ones you use daily (+, -, *, /, ==, !=, &&, ||) within your first week of writing code.
1. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 10 / 3 | 3 (integer division) |
% | Modulus (remainder) | 10 % 3 | 1 |
1int a = 10;
2int b = 3;
3
4System.out.println(a + b); // 13
5System.out.println(a - b); // 7
6System.out.println(a * b); // 30
7System.out.println(a / b); // 3 ← integer division, decimal dropped
8System.out.println(a % b); // 1 ← remainder of 10 ÷ 3Integer Division — The Most Common Gotcha
When both operands are int, division produces an int — the decimal part is silently dropped.
1System.out.println(10 / 3); // 3 (not 3.333...)
2System.out.println(7 / 2); // 3 (not 3.5)
3System.out.println(1 / 4); // 0 (not 0.25)To get a decimal result, at least one operand must be a double:
1System.out.println(10.0 / 3); // 3.3333333333333335
2System.out.println((double) 10 / 3); // 3.3333333333333335
3System.out.println(10 / 3.0); // 3.3333333333333335🔗 Full explanation of this with casting: → Type Casting →
The Modulus Operator %
% gives the remainder after division — not the quotient.
1System.out.println(10 % 3); // 1 (10 = 3×3 + 1)
2System.out.println(15 % 5); // 0 (15 = 5×3 + 0, divides evenly)
3System.out.println(7 % 2); // 1 (7 = 2×3 + 1)Real-world uses of %:
1// Check if a number is even or odd:
2if (number % 2 == 0) {
3 System.out.println("Even");
4} else {
5 System.out.println("Odd");
6}
7
8// Check if divisible by 5:
9if (number % 5 == 0) {
10 System.out.println("Divisible by 5");
11}
12
13// Wrap around — e.g. clock hours (0 to 11):
14int hour = (currentHour + 5) % 12;String Concatenation with +
When + is used with a String, it concatenates (joins) instead of adding:
1String name = "Arjun";
2int age = 22;
3
4System.out.println("Name: " + name); // Name: Arjun
5System.out.println("Age: " + age); // Age: 22
6System.out.println("Sum: " + (10 + 20)); // Sum: 30
7System.out.println("Sum: " + 10 + 20); // Sum: 1020 ← concatenation, not addition!⚠️ When a String appears on either side of +, everything becomes concatenation. Use parentheses to force addition first: "Sum: " + (10 + 20).
2. Assignment Operators
Assignment operators store a value into a variable. The basic one is =, but Java has shorthand versions that combine an operation with assignment.
| Operator | Meaning | Example | Equivalent To |
|---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 2 | x = x - 2 |
*= | Multiply and assign | x *= 4 | x = x * 4 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
1int x = 10;
2
3x += 5;
4System.out.println(x); // 15
5
6x -= 3;
7System.out.println(x); // 12
8
9x *= 2;
10System.out.println(x); // 24
11
12x /= 4;
13System.out.println(x); // 6
14
15x %= 4;
16System.out.println(x); // 2These shorthand operators are used constantly — especially += and -= in loops and counters.
3. Comparison Operators
Comparison operators compare two values and always return a boolean result — either true or false.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 7 > 4 | true |
< | Less than | 3 < 8 | true |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 4 <= 3 | false |
1int a = 10;
2int b = 20;
3
4System.out.println(a == b); // false
5System.out.println(a != b); // true
6System.out.println(a > b); // false
7System.out.println(a < b); // true
8System.out.println(a >= 10); // true
9System.out.println(b <= 20); // trueComparison operators are the backbone of if statements and loops:
1int score = 75;
2
3if (score >= 90) {
4 System.out.println("Grade: A");
5} else if (score >= 75) {
6 System.out.println("Grade: B"); // ← this prints
7} else {
8 System.out.println("Grade: C");
9}⚠️ == vs = — the most common beginner mistake:
- ›
=assigns a value:x = 5puts 5 into x - ›
==compares two values:x == 5asks "is x equal to 5?"
Using = inside an if condition is a logic error — and one of the most common mistakes in all of programming.
⚠️ Never use == to compare Strings. Use .equals() instead:
1String name = "Arjun";
2name == "Arjun" // ❌ may give wrong result
3name.equals("Arjun") // ✅ always correct4. Logical Operators
Logical operators combine multiple conditions into one. They always return a boolean.
| Operator | Name | Meaning | Example |
|---|---|---|---|
&& | AND | Both conditions must be true | age > 18 && hasId |
|| | OR | At least one condition must be true | isAdmin || isOwner |
! | NOT | Reverses the boolean value | !isLoggedIn |
1int age = 20;
2boolean hasId = true;
3
4// AND — both conditions must be true
5if (age >= 18 && hasId) {
6 System.out.println("Entry allowed"); // ✅ prints — both true
7}
8
9// OR — at least one must be true
10boolean isWeekend = false;
11boolean isHoliday = true;
12
13if (isWeekend || isHoliday) {
14 System.out.println("Day off!"); // ✅ prints — isHoliday is true
15}
16
17// NOT — reverses a condition
18boolean isLoggedIn = false;
19
20if (!isLoggedIn) {
21 System.out.println("Please log in"); // ✅ prints — !false = true
22}Short-Circuit Evaluation
&& and || use short-circuit evaluation — they stop evaluating as soon as the result is determined.
1// && stops at the first false — no need to check the rest
2false && someExpression // someExpression is NEVER evaluated
3
4// || stops at the first true — no need to check the rest
5true || someExpression // someExpression is NEVER evaluatedThis matters in practice:
1String name = null;
2
3// Safe — short-circuit prevents NullPointerException:
4if (name != null && name.equals("Arjun")) {
5 System.out.println("Hello Arjun");
6}
7// If name is null, 'name != null' is false → && stops → name.equals() never runs5. Unary Operators
Unary operators work on a single operand — no second value needed.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Unary plus | +5 | 5 (no change) |
- | Unary minus | -5 | Negates the value |
++ | Increment | x++ or ++x | Adds 1 to x |
-- | Decrement | x-- or --x | Subtracts 1 from x |
! | Logical NOT | !true | false |
Increment and Decrement — Pre vs Post
++ and -- come in two flavors — and the difference matters when used in expressions:
1int x = 5;
2
3// Post-increment: use THEN increment
4int a = x++;
5System.out.println(a); // 5 ← original value used
6System.out.println(x); // 6 ← x is now 6
7
8// Pre-increment: increment THEN use
9int y = 5;
10int b = ++y;
11System.out.println(b); // 6 ← incremented value used
12System.out.println(y); // 6 ← y is now 6💡 In simple statements, pre and post make no difference:
1count++; // same as
2++count; // both just add 1 to countThe difference only matters when ++ or -- is used inside a larger expression like a = x++.
6. Ternary Operator
The ternary operator is a compact one-line if/else. It takes three operands — hence "ternary."
Syntax:
1variable = (condition) ? valueIfTrue : valueIfFalse;1int age = 20;
2String status = (age >= 18) ? "Adult" : "Minor";
3System.out.println(status); // AdultThis is exactly equivalent to:
1String status;
2if (age >= 18) {
3 status = "Adult";
4} else {
5 status = "Minor";
6}More examples:
1int a = 15, b = 20;
2int max = (a > b) ? a : b;
3System.out.println("Max: " + max); // Max: 20
4
5int score = 72;
6String grade = (score >= 90) ? "A" : (score >= 75) ? "B" : "C";
7System.out.println(grade); // C💡 When to use ternary: Use it for simple assignments where both branches are a single value. Avoid it for complex logic — nested ternaries (? : ? :) are hard to read and should be replaced with if/else.
Operator Precedence
When multiple operators appear in one expression, Java follows a specific order — like BODMAS/PEMDAS in mathematics.
1// Without precedence knowledge — surprising results:
2int result = 2 + 3 * 4;
3System.out.println(result); // 14, not 20
4// Because: 3 * 4 = 12 first, then 2 + 12 = 14
5
6// Use parentheses to control order:
7int result = (2 + 3) * 4;
8System.out.println(result); // 20 ✅
9
10// Mixed example:
11boolean check = 5 + 3 > 2 * 4 && 10 != 9;
12// Step 1: 5+3=8, 2*4=8 (arithmetic first)
13// Step 2: 8>8 = false (comparison)
14// Step 3: 10!=9 = true (comparison)
15// Step 4: false && true = false (logical)
16System.out.println(check); // false💡 Practical rule: When in doubt, use parentheses. They make intent clear and prevent precedence surprises. (a + b) * c is always clearer than relying on the reader to remember precedence rules.
A Complete Example — All Operators Together
1public class OperatorsDemo {
2 public static void main(String[] args) {
3
4 // Arithmetic
5 int a = 17, b = 5;
6 System.out.println("17 + 5 = " + (a + b)); // 22
7 System.out.println("17 - 5 = " + (a - b)); // 12
8 System.out.println("17 * 5 = " + (a * b)); // 85
9 System.out.println("17 / 5 = " + (a / b)); // 3 (integer division)
10 System.out.println("17 % 5 = " + (a % b)); // 2 (remainder)
11
12 // Assignment shorthand
13 int x = 10;
14 x += 5; System.out.println("x after +=5: " + x); // 15
15 x *= 2; System.out.println("x after *=2: " + x); // 30
16 x -= 6; System.out.println("x after -=6: " + x); // 24
17 x /= 4; System.out.println("x after /=4: " + x); // 6
18
19 // Comparison
20 System.out.println("17 > 5: " + (a > b)); // true
21 System.out.println("17 == 5: " + (a == b)); // false
22 System.out.println("17 != 5: " + (a != b)); // true
23
24 // Logical
25 boolean big = a > 10; // true
26 boolean small = b < 3; // false
27 System.out.println("big && small: " + (big && small)); // false
28 System.out.println("big || small: " + (big || small)); // true
29 System.out.println("!big: " + (!big)); // false
30
31 // Increment
32 int count = 0;
33 count++;
34 count++;
35 count++;
36 System.out.println("count: " + count); // 3
37
38 // Ternary
39 String label = (a > b) ? "a is bigger" : "b is bigger";
40 System.out.println(label); // a is bigger
41 }
42}Common Operator Mistakes
1. Using = instead of == in conditions
1if (x = 5) { } // ❌ error — assigns 5 to x, not a comparison
2if (x == 5) { } // ✅ compares x to 52. Integer division losing the decimal
1double avg = 7 / 2; // ❌ 3.0 — integer division first
2double avg = (double) 7 / 2; // ✅ 3.53. Comparing Strings with ==
1String s = "hello";
2if (s == "hello") { } // ❌ unreliable
3if (s.equals("hello")) { } // ✅ always correct4. Confusing && with ||
1// Age must be between 18 AND 60:
2if (age >= 18 && age <= 60) { } // ✅ correct
3
4// Wrong — can never be both < 18 AND > 60 at the same time:
5if (age < 18 && age > 60) { } // ❌ always false — nothing satisfies this
6if (age < 18 || age > 60) { } // ✅ correct for "outside range"5. Not using parentheses with mixed operators
1boolean result = true || false && false;
2// Reads as: true || (false && false) = true || false = true
3// Precedence: && before ||. Add parentheses to make intent clear.Quick Reference — All Operators
| Category | Operators |
|---|---|
| Arithmetic | + - * / % |
| Assignment | = += -= *= /= %= |
| Comparison | == != > < >= <= |
| Logical | && || ! |
| Unary | + - ++ -- ! |
| Ternary | ? : |
The 3 Things to Remember From This Page
- ›Integer division drops the decimal —
7 / 2 = 3, not3.5. Cast one operand todoubleto get the decimal:(double) 7 / 2 = 3.5 - ›
=assigns,==compares — using=inside anifcondition is one of the most common bugs in all of programming. Never compare with=. - ›Use parentheses when mixing operators — don't rely on precedence rules in complex expressions. Parentheses make your intent explicit and your code readable.
Summary
- ›Arithmetic:
+-*/%— integer division truncates,%gives remainder - ›Assignment:
=and shorthands+=-=*=/=%= - ›Comparison:
==!=><>=<=— always returnboolean - ›Logical:
&&(both true),||(one true),!(flip) — use short-circuit evaluation - ›Unary:
++--increment/decrement — pre vs post matters inside expressions - ›Ternary:
condition ? valueIfTrue : valueIfFalse— compact one-line if/else - ›Precedence:
()→ unary →* / %→+ -→ comparison → equality →&& ||→?:→=
What to Read Next
| Topic | Link |
|---|---|
| Type casting — fixing integer division | Type Casting → |
| if/else — using comparison and logical operators | if / else / else-if → |
for loop — using ++ and comparison operators | for Loop → |
String operations with + and methods | String Basics → |
| User input — reading values to operate on | User Input (Scanner) → |