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

Updated 28 Oct 20266 min read

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:

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

java
byte count = 10;
count += 5;        // compiles, count becomes 15
// count = count + 5;  // does not compile: count + 5 has type int

Unary operators and pre-increment versus post-increment

Post-increment contributes the old value. Pre-increment changes it first:

java
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;  // true

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

java
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 1 only where both bits are 1: 1010 & 0110 = 0010, which is 2.

  • OR keeps a 1 where either bit is 1: 1010 | 0110 = 1110, which is 14.

  • XOR keeps a 1 where the bits differ: 1010 ^ 0110 = 1100, which is 12.

Bit grid comparing flags = 10 (1010) with mask = 6 (0110), showing AND = 0010 (2), OR = 1110 (14) and XOR = 1100 (12).

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:

java
System.out.println(2 + 3 + " Java");      // 5 Java
System.out.println("Java " + 2 + 3);      // Java 23
System.out.println("Java " + (2 + 3));    // Java 5

The first line adds first. In the second, the initial string makes later + signs concatenate. Parentheses restore addition.

Now create two separate objects:

java
String first = new String("Java");
String second = new String("Java");
System.out.println(first == second);       // false
System.out.println(first.equals(second));  // true

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

Precedence tree for 18 / 3 + 2 * 5 - 4 % 3, resolving 6, 10 and 1, then 6 + 10 = 16 and finally 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.

  1. 9 + 12 / 3 * 2 is 17: 12 / 3 = 4, 4 * 2 = 8, then 9 + 8 = 17.

  2. With int x = 4; int y = x++ * 2 + ++x;, x++ contributes 4 and leaves x = 5; ++x contributes 6. Thus y = 4 * 2 + 6 = 14, and x = 6.

  3. With int n = 0; boolean ok = n > 0 && 30 / n > 3;, ok is false with no exception.

  4. For int a = 12; int b = 5;, binary 1100 and 0101 give a & b = 4, a | b = 13, and a ^ b = 9.

  5. "Score: " + 4 + 6 prints Score: 46; "Score: " + (4 + 6) prints Score: 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.