Learning operators in Java is one of the most essential and frequently used skills in any Java tutorial for beginners — operators are the symbols that perform actions on variables and values (addition, comparison, logical decisions, assignment, etc.), and mastering them lets you write clean, efficient, and readable code. Java has three main categories of operators based on the number of operands: unary (1 operand), binary (2 operands), and ternary (3 operands), plus special rules for operator precedence that determine the order of evaluation when multiple operators appear in one expression.

In this complete operators in Java tutorial, you’ll learn:

  • All common unary operators (++, –, !)
  • Arithmetic, assignment, compound assignment, relational, logical, and bitwise binary operators
  • The powerful ternary (conditional) operator
  • Operator precedence rules and how to avoid confusion
  • Real-world examples in jshell (Java’s interactive REPL)

All examples are tested on Java 21 (LTS in 2025–2026) — copy-paste them into jshell to see instant results.

Prerequisites

  • Java 11+ installed (preferably 17 or 21 LTS)
  • Terminal + jshell (type jshell to start)
  • Basic knowledge of variables & data types

1. Unary Operators – One Operand in Operators in Java

Unary operators work on a single value.

Increment / Decrement (++, –)

				
					int x = 5;
System.out.println("Pre-increment: " + ++x);   // 6 (increments first)
System.out.println("Post-increment: " + x++);  // 6 (prints then increments)
System.out.println("After post: " + x);        // 7

int y = 10;
System.out.println(--y);                       // 9
System.out.println(y--);                       // 9
System.out.println(y);                         // 8
				
			

Logical NOT (!) – flips boolean

				
					boolean isJavaFun = true;
System.out.println(!isJavaFun);                // false

boolean isRaining = false;
if (!isRaining) {
    System.out.println("Go outside!");
}
				
			

2. Binary Operators – Two Operands in Operators in Java

Arithmetic

				
					int a = 10;
int b = 3;
System.out.println(a + b);   // 13
System.out.println(a - b);   // 7
System.out.println(a * b);   // 30
System.out.println(a / b);   // 3 (integer division)
System.out.println(a % b);   // 1 (remainder)
				
			

Compound Assignment (shorthand)

				
					int score = 100;
score += 50;     // score = score + 50 → 150
score -= 20;     // 130
score *= 2;      // 260
score /= 10;     // 26
score %= 7;      // 5
				
			

Relational (return boolean)

				
					int p = 10;
int q = 20;
System.out.println(p == q);   // false
System.out.println(p != q);   // true
System.out.println(p > q);    // false
System.out.println(p <= q);   // true
				
			

instanceof – check object type

				
					String name = "Zain";
System.out.println(name instanceof String);     // true
System.out.println(name instanceof Object);     // true
				
			

Logical (AND, OR, XOR)

				
					boolean hasCoffee = true;
boolean hasTime = false;

System.out.println(hasCoffee && hasTime);   // false (both must be true)
System.out.println(hasCoffee || hasTime);   // true (at least one true)
System.out.println(hasCoffee ^ hasTime);    // true (one true, one false)
				
			

Short-circuit (&& and ||) – skip second operand if possible

				
					String text = null;
if (text != null && text.length() > 0) {    // safe – doesn't evaluate length() if null
    System.out.println("Not empty");
}
				
			

3. Ternary Operator – The Only 3-Operand Operator in Java

Syntax: condition ? valueIfTrue : valueIfFalse

				
					int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println("You are: " + status);   // You are: Adult

boolean isRaining = true;
String plan = isRaining ? "Stay inside" : "Go for a walk";
System.out.println(plan);
				
			

Very common for concise conditional assignment.

4. Operator Precedence – Order of Evaluation

Java evaluates operators in a specific order (highest to lowest precedence):

  1. Postfix ++ —
  2. Prefix ++ — !
    • / %
      •  
  3. < <= > >= instanceof
  4. == !=
  5. & (bitwise AND)
  6. ^ (XOR)
  7. | (bitwise OR)
  8. &&
  9. ||
  10. ?: (ternary)
  11. = += -= *= /= %=

Example:

				
					int result = 10 + 5 * 2;          // 20 (multiplication first)
int result2 = (10 + 5) * 2;       // 30 (parentheses override)
				
			

Best practice: Use parentheses to make precedence explicit — clean code > clever code.

5. Best Practices & Modern Tips (2025–2026)

  • Prefer ++x or x++ only when it improves readability
  • Use && and || (short-circuit) instead of & and in conditions
  • Avoid deep nesting of ternary operators — use if for clarity
  • Always use parentheses in complex expressions
  • Use meaningful variable names — isAdult instead of b
  • Avoid mixing types without explicit casting

Operators in Java – FAQ (2025–2026)

  1. What are the main operators in Java?
    Unary (++, –, !), Binary (arithmetic, relational, logical, assignment), Ternary (?:)
  2. What’s the difference between ++x and x++?
    ++x increments first, then uses value; x++ uses value, then increments.
  3. How does short-circuit work in operators in Java?
    && skips right if left is false; || skips right if left is true.
  4. What is the ternary operator in Java?
    condition ? valueIfTrue : valueIfFalse — concise if-else
  5. How do I control order of operators in Java?
    Use parentheses — they override precedence.

Summary

You now fully understand operators in Java: unary (++, –, !), binary (arithmetic, relational, logical, assignment), ternary (?:), precedence rules, and best practices.

Mastering operators in Java lets you write concise, efficient, and readable code — the foundation of every real Java program.