Operators in Java: Types, Precedence and Runnable Examples
Learn how Java evaluates arithmetic, assignment, unary, logical, bitwise and ternary operators through code and step-by-step output traces.
KnowledgeGate Team
Exam prep & CS education

Java operators look familiar, but operand types, evaluation order and short-circuit rules decide the result. Java operators range from arithmetic and precedence to bitwise work and output tracing. Calculate each result before checking the output.
What operators do inside a Java expression
In int total = 8 + 3 * 2;, + and * are operators, and the numbers are operands. The right side is an expression. Multiplication runs first: 3 * 2 = 6, then total = 8 + 6 = 14.
Arity is the operand count. Unary -5 has one, binary 8 + 3 has two, and marks >= 40 ? "Pass" : "Retry" has three parts. The ?: pair is one ternary operator.
Family | Common operators |
|---|---|
Arithmetic |
|
Unary |
|
Comparison |
|
Logical and bitwise |
|
Shifts |
|
Assignment |
|
Conditional |
|
Arithmetic and assignment with a runnable sharing example
Run this complete program:
public class OperatorDemo {
public static void main(String[] args) {
int items = 23;
int people = 4;
System.out.println(items + people); // 27
System.out.println(items - people); // 19
System.out.println(items * people); // 92
System.out.println(items / people); // 5
System.out.println(items % people); // 3
double misleading = items / people;
double precise = (double) items / people;
System.out.println(misleading); // 5.0
System.out.println(precise); // 5.75
int score = 40;
score += 12;
score -= 7;
System.out.println(score); // 45
}
}Two int operands make 23 / 4 equal 5. misleading then stores it as 5.0. The cast changes an operand before division, making precise equal 5.75.
The score trace is 40 -> 52 -> 45. Notice this compound-assignment type rule:
byte count = 10;
count += 5; // compiles, count becomes 15
// count = count + 5; // does not compile: count + 5 has type intUnary operators and pre-increment versus post-increment
Post-increment contributes the old value. Pre-increment changes it first:
int post = 6;
int postValue = post++; // postValue = 6, post = 7
int pre = 6;
int preValue = ++pre; // preValue = 7, pre = 7
int temperature = 4;
int belowZero = -temperature; // -4
boolean loggedIn = false;
boolean needsLogin = !loggedIn; // trueNow trace int a = 5; int b = a++ + ++a;. Java evaluates left to right. The a++ contributes 5 and leaves a = 6. Then ++a makes and contributes 7. Therefore, b = 12 and final a = 7.
That evaluation order is defined by Oracle's Java Language Specification, Chapter 15. In production, give each state change its own statement.
Comparison, logical operators and short-circuit safety
With int age = 21; boolean hasId = true;, age >= 18 is true, age < 18 is false, and age >= 18 && hasId is true. The negation !(age >= 18) is false.
Short-circuiting can also make an expression safe:
int divisor = 0;
boolean safe = divisor != 0 && 20 / divisor > 2;The left side is false, so && skips division. safe is false, with no ArithmeticException. Using & evaluates both sides, so division by zero throws.
Likewise, boolean canEnter = hasId || age >= 25; is true without the right side. Operators && and || short-circuit. Boolean & and | evaluate both sides and also work on integer bits.
Bitwise and shift operators using one 4-bit flag set
Take int flags = 10 (1010) and int mask = 6 (0110).
AND keeps a
1only where both bits are1:1010 & 0110 = 0010, which is2.OR keeps a
1where either bit is1:1010 | 0110 = 1110, which is14.XOR keeps a
1where the bits differ:1010 ^ 0110 = 1100, which is12.

Java int uses 32 bits, so these forms are shorthand. Across 32 bits, ~10 = -11. Also, 10 << 1 = 20, 10 >> 1 = 5, and 10 >>> 1 = 5. The >> operator copies the sign bit; >>> inserts zero bits. They differ for negatives.
The ternary operator, string concatenation and the == trap
In int marks = 72; String result = marks >= 40 ? "Pass" : "Retry";, the true condition assigns "Pass". Prefer if statements to unreadable nested ternaries.
String concatenation follows evaluation order:
System.out.println(2 + 3 + " Java"); // 5 Java
System.out.println("Java " + 2 + 3); // Java 23
System.out.println("Java " + (2 + 3)); // Java 5The first line adds first. In the second, the initial string makes later + signs concatenate. Parentheses restore addition.
Now create two separate objects:
String first = new String("Java");
String second = new String("Java");
System.out.println(first == second); // false
System.out.println(first.equals(second)); // trueDifferent references make == false. Their character content matches, so .equals() is true. Java string handling explains the string pool behind that. Review these object-oriented programming concepts if references and instances are unclear.
Precedence, associativity and output-tracing traps
For int answer = 18 / 3 + 2 * 5 - 4 % 3;, division, multiplication and remainder run first: 6 + 10 - 1. Then work left to right: 6 + 10 = 16, followed by 16 - 1 = 15.

With parentheses, (18 / (3 + 2)) * (5 - 4) becomes (18 / 5) * 1, then 3 * 1 = 3 after integer division.
When precedence ties, 10 - 3 - 2 groups as (10 - 3) - 2 = 5, not 10 - (3 - 2) = 9. Assignment groups right to left, so int p; int q; p = q = 7; leaves both at 7.
From high to low: postfix; unary; multiplicative; additive; shifts; relational; equality; bitwise AND, XOR, OR; &&; ||; ?:; assignment. Questions may test output, type errors, && versus &, reference equality or parentheses. Oracle's specification defines this ordering.
Five Java operator exercises
Predict before reading each trace.
9 + 12 / 3 * 2is17:12 / 3 = 4,4 * 2 = 8, then9 + 8 = 17.With
int x = 4; int y = x++ * 2 + ++x;,x++contributes4and leavesx = 5;++xcontributes6. Thusy = 4 * 2 + 6 = 14, andx = 6.With
int n = 0; boolean ok = n > 0 && 30 / n > 3;,okisfalsewith no exception.For
int a = 12; int b = 5;, binary1100and0101givea & b = 4,a | b = 13, anda ^ b = 9."Score: " + 4 + 6printsScore: 46;"Score: " + (4 + 6)printsScore: 10.
The short version and next step
Operand types decide how an operator behaves.
Integer division drops the fractional part.
&&and||short-circuit.Clear parentheses beat fragile memorisation.
Use
.equals()for string content.
Continue through the language with the Java Programming course. Once the basics are steady, use DSA Using Java for later practice. Compare the broader path in the Coding and DSA learning category.
Keep learning

HashMap Internals in Java: Buckets, Collisions, Resizing and Treeification
Trace put() and get() from a key's hash code to its bucket, then see exactly how replacement, resizing, tree bins, and mutable keys behave.

Java Multithreading Basics: Threads, Runnable and Safe Counters with Examples
Build two Java thread examples, trace a lost counter update exactly, and learn when to use synchronized, AtomicInteger, volatile or private state.

Static and Final in Java: Shared State, Constants and Override Rules with Runnable Examples
Learn what static and final control in Java by tracing a shared counter, class initialisation, final references, constants and inheritance errors.

Polymorphism in Java: Overloading, Overriding and Runnable Examples
Learn how Java chooses overloaded methods at compile time and overridden methods at runtime. Trace two complete programs, common traps and practice targets.

