Java Tutorial
🔍

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.

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division10 / 33 (integer division)
%Modulus (remainder)10 % 31
Java
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 ÷ 3

Integer Division — The Most Common Gotcha

When both operands are int, division produces an int — the decimal part is silently dropped.

Java
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:

Java
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.

Java
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 %:

Java
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:

Java
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.

OperatorMeaningExampleEquivalent To
=Assignx = 5x = 5
+=Add and assignx += 3x = x + 3
-=Subtract and assignx -= 2x = x - 2
*=Multiply and assignx *= 4x = x * 4
/=Divide and assignx /= 2x = x / 2
%=Modulus and assignx %= 3x = x % 3
Java
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); // 2

These 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.

OperatorMeaningExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than7 > 4true
<Less than3 < 8true
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 3false
Java
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); // true

Comparison operators are the backbone of if statements and loops:

Java
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 = 5 puts 5 into x
  • == compares two values: x == 5 asks "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:

Java
1String name = "Arjun"; 2name == "Arjun" // ❌ may give wrong result 3name.equals("Arjun") // ✅ always correct

4. Logical Operators

Logical operators combine multiple conditions into one. They always return a boolean.

OperatorNameMeaningExample
&&ANDBoth conditions must be trueage > 18 && hasId
||ORAt least one condition must be trueisAdmin || isOwner
!NOTReverses the boolean value!isLoggedIn
LOGICAL OPERATORS — TRUTH TABLE && (AND) Both must be true true && true = true true && false = false false && true = false false && false = false || (OR) At least one must be true true || true = true true || false = true false || true = true false || false = false ! (NOT) Flips the value !true = false !false = true
Java
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.

Java
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 evaluated

This matters in practice:

Java
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 runs

5. Unary Operators

Unary operators work on a single operand — no second value needed.

OperatorNameExampleResult
+Unary plus+55 (no change)
-Unary minus-5Negates the value
++Incrementx++ or ++xAdds 1 to x
--Decrementx-- or --xSubtracts 1 from x
!Logical NOT!truefalse

Increment and Decrement — Pre vs Post

++ and -- come in two flavors — and the difference matters when used in expressions:

Java
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:

Java
1count++; // same as 2++count; // both just add 1 to count

The 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:

Java
1variable = (condition) ? valueIfTrue : valueIfFalse;
Java
1int age = 20; 2String status = (age >= 18) ? "Adult" : "Minor"; 3System.out.println(status); // Adult

This is exactly equivalent to:

Java
1String status; 2if (age >= 18) { 3 status = "Adult"; 4} else { 5 status = "Minor"; 6}

More examples:

Java
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.

OPERATOR PRECEDENCE (HIGH → LOW) 1 ( ) Parentheses 2 ++ -- ! (unary) 3 * / % (multiplication, division, modulus) 4 + - (addition, subtraction) 5 < > <= >= (comparison) 6 == != (equality) 7+ && || ?: = (logical, ternary, assignment)
Java
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

Java
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

Java
1if (x = 5) { } // ❌ error — assigns 5 to x, not a comparison 2if (x == 5) { } // ✅ compares x to 5

2. Integer division losing the decimal

Java
1double avg = 7 / 2; // ❌ 3.0 — integer division first 2double avg = (double) 7 / 2; // ✅ 3.5

3. Comparing Strings with ==

Java
1String s = "hello"; 2if (s == "hello") { } // ❌ unreliable 3if (s.equals("hello")) { } // ✅ always correct

4. Confusing && with ||

Java
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

Java
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

CategoryOperators
Arithmetic+ - * / %
Assignment= += -= *= /= %=
Comparison== != > < >= <=
Logical&& || !
Unary+ - ++ -- !
Ternary? :

The 3 Things to Remember From This Page

  1. Integer division drops the decimal7 / 2 = 3, not 3.5. Cast one operand to double to get the decimal: (double) 7 / 2 = 3.5
  2. = assigns, == compares — using = inside an if condition is one of the most common bugs in all of programming. Never compare with =.
  3. 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 return boolean
  • 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

TopicLink
Type casting — fixing integer divisionType Casting →
if/else — using comparison and logical operatorsif / else / else-if →
for loop — using ++ and comparison operatorsfor Loop →
String operations with + and methodsString Basics →
User input — reading values to operate onUser Input (Scanner) →
Operators | DevStackFlow