Java Operators: Complete Practical Guide with Precedence Table

An operator is a specialized symbol that instructs the compiler to execute specific mathematical, relational, or logical manipulations on one, two, or three operands.

Categories of Java Operators

1. Arithmetic Operators

Used to perform fundamental arithmetic operations:

Operator Description Example (a = 15, b = 4)
+Additiona + b → 19
-Subtractiona - b → 11
*Multiplicationa * b → 60
/Division (Integer truncates)a / b → 3 (15 / 4 = 3)
%Modulus (Remainder)a % b → 3 (15 % 4 = 3 remainder)

2. Relational / Comparison Operators

Compare two operands and always yield a boolean result (true or false):

  • == (Equal to): 10 == 10 → true
  • != (Not equal to): 10 != 5 → true
  • > and >= (Greater than / or equal): 15 >= 15 → true
  • < and <= (Less than / or equal): 8 <= 12 → true

3. Logical Operators & Short-Circuit Evaluation

Used to combine multiple boolean conditions:

int age = 22;
boolean hasLicense = true;

// Short-Circuit AND (&&): Both must be true
if (age >= 18 && hasLicense) {
    System.out.println("Eligible to Drive!");
}

// Short-Circuit OR (||): At least one must be true
boolean hasPassport = false;
boolean hasVoterId = true;
if (hasPassport || hasVoterId) {
    System.out.println("Identity Verified!");
}

// Logical NOT (!): Inverts boolean state
boolean isBlocked = false;
if (!isBlocked) {
    System.out.println("Access Granted!");
}

4. Ternary Operator (Conditional Operator)

A concise one-line alternative to simple if-else statements:

int marks = 78;
// Syntax: condition ? value_if_true : value_if_false
String result = (marks >= 40) ? "PASS" : "FAIL";
System.out.println("Student Status: " + result); // Output: PASS

Java Operator Precedence & Associativity Table

When an expression contains multiple operators, precedence determines evaluation order:

Precedence Level Operators Associativity
1 (Highest) Postfix: expr++, expr--, [], ., () Left to Right
2 Unary: ++expr, --expr, +, -, !, ~ Right to Left
3 Multiplicative: *, /, % Left to Right
4 Additive: +, - Left to Right
5 Shift: <<, >>, >>> Left to Right
6 Relational: <, >, <=, >=, instanceof Left to Right
7 Equality: ==, != Left to Right
8 Logical AND: && Left to Right
9 Logical OR: || Left to Right
10 Ternary: ? : Right to Left
11 (Lowest) Assignment: =, +=, -=, *=, /=, %= Right to Left

University Exam Corner (BCA / B.Sc CS)

Exam Tracing Problem: "What is the output of the following Java snippet?"

int a = 5;
int b = a++ + ++a * 2;
System.out.println("a=" + a + ", b=" + b);

Step-by-Step Dry Run:
1. a++ yields 5 (and increments a to 6).
2. Next, ++a increments a from 6 to 7 and yields 7.
3. Multiplication has higher precedence than addition: 7 * 2 = 14.
4. Addition: 5 + 14 = 19.
Final Result: a=7, b=19.

Next Step

Now let us explore Java comments and industry-standard Javadoc documentation. Proceed to: Java Comments & Javadoc Documentation →

Frequently Asked Questions (FAQ)

Q1. What is the difference between & and && in Java?
&& is the short-circuit logical AND operator: if the left operand evaluates to false, the right operand is never executed. & is the standard bitwise / logical AND operator: it always evaluates both operands regardless of the left condition.
Q2. What is the difference between prefix (++x) and postfix (x++) increment?
Prefix (++x) increments the variable's value first before returning the updated value in the expression. Postfix (x++) evaluates and yields the original value first, and only increments the variable afterwards.
Q3. How does the ternary operator work in Java?
The ternary operator is a shorthand conditional expression with the syntax: 'condition ? expressionIfTrue : expressionIfFalse'. It evaluates the condition and returns either the first or second expression.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java If-Else Branching →

Using operators in conditional logic.

Java Type Casting →

Operator numeric promotion.

Java Switch Statements →

Multi-branch decision making.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!