Skip to main content
An operator in Java is a special symbol that directs the compiler to perform specific mathematical, relational, logical, or bitwise operations on one, two, or three operands, producing a deterministic result. Operators are characterized by their arity (unary, binary, ternary), precedence, and associativity.

Classification by Arity

  • Unary: Operates on a single operand.
  • Binary: Operates on two operands.
  • Ternary: Operates on three operands.

Operator Categories and Syntax

Unary Operators

Require a single operand. While all are unary by arity, they are distinguished by their syntax position (postfix vs. prefix) and precedence level. Postfix operators associate left-to-right, while prefix and other unary operators associate right-to-left.
OperatorDescription
expr++, expr--Post-increment / Post-decrement (evaluates, then mutates)
++expr, --exprPre-increment / Pre-decrement (mutates, then evaluates)
+expr, -exprUnary plus (promotes type) / Unary minus (arithmetic negation)
!exprLogical complement (inverts boolean value)
~exprBitwise complement (inverts bits of integer types)
(Type)Type cast (explicitly converts operand to specified type)
public class UnaryExample {
    public static void main(String[] args) {
        int a = 5;
        System.out.println(a++);        // 5 (Evaluates to 5, then a becomes 6)
        System.out.println(++a);        // 7 (a becomes 7, then evaluates to 7)
        System.out.println(-a);         // -7
        System.out.println(!false);     // true
        System.out.println(~0);         // -1 (Bitwise inversion of all 0s to all 1s)
        System.out.println((double) a); // 7.0
    }
}

Arithmetic Operators

Perform standard mathematical computations. While they primarily operate on numeric types, the additive operator (+) is overloaded for String concatenation. If at least one operand is a String, the + operator converts the other operand to a String and concatenates them.
OperatorDescription
+, -Addition (or String concatenation) / Subtraction
*, /, %Multiplication / Division / Modulo (Remainder)
public class ArithmeticExample {
    public static void main(String[] args) {
        System.out.println(10 + 5);    // 15
        System.out.println(10 / 3);    // 3 (Integer division truncates decimal)
        System.out.println(10 % 3);    // 1
        System.out.println("Val: " + 5); // Val: 5
    }
}

Relational and Equality Operators

Evaluate the magnitude or equivalence relationship between two operands, strictly returning a boolean value.
OperatorDescription
>, >=, <, <=Greater than / Greater than or equal to / Less than / Less than or equal to
==, !=Equal to / Not equal to
public class RelationalExample {
    public static void main(String[] args) {
        System.out.println(5 > 3);  // true
        System.out.println(5 <= 3); // false
        System.out.println(5 == 5); // true
        System.out.println(5 != 3); // true
    }
}

Logical Operators

Perform Boolean logic operations. The conditional operators (&&, ||) exhibit short-circuiting behavior, meaning the second operand is not evaluated if the first operand determines the overall result.
OperatorDescription
&&, ``Conditional-AND / Conditional-OR (Short-circuiting)
&, ``Boolean logical AND / OR (Non-short-circuiting)
^Boolean logical exclusive OR (XOR)
public class LogicalExample {
    public static void main(String[] args) {
        System.out.println(true && false); // false
        System.out.println(true || false); // true
        System.out.println(true ^ true);   // false
    }
}

Bitwise and Bit Shift Operators

Operate directly on the binary representations of integer types (byte, short, int, long, char).
OperatorDescription
&, `, ^`Bitwise AND / Inclusive OR / Exclusive OR
<<Signed left shift (shifts bits left, fills with 0)
>>Signed right shift (shifts bits right, preserves sign bit)
>>>Unsigned right shift (shifts bits right, fills with 0)
public class BitwiseExample {
    public static void main(String[] args) {
        System.out.println(5 & 3);     // 1 (0101 & 0011 = 0001)
        System.out.println(5 | 3);     // 7 (0101 | 0011 = 0111)
        System.out.println(5 ^ 3);     // 6 (0101 ^ 0011 = 0110)
        System.out.println(16 >> 1);   // 8
        System.out.println(-16 >>> 1); // 2147483640
    }
}

Assignment Operators

Assign the evaluated value of the right operand to the left operand. Compound assignment operators perform the specified operation and an implicit type cast to the type of the left-hand variable.
OperatorDescription
=Simple assignment
+=, -=, *=, /=, %=Arithmetic compound assignment
&=, `=, ^=, <code>&lt;&lt;=</code>, >>=, >>>=`Bitwise/Shift compound assignment
public class AssignmentExample {
    public static void main(String[] args) {
        int x = 10;
        x += 5; 
        System.out.println(x); // 15
        x %= 4;
        System.out.println(x); // 3
    }
}

Ternary Operator

The only operator in Java that takes three operands. It evaluates a boolean condition and returns one of two expressions based on the result.
OperatorDescription
? :condition ? exprIfTrue : exprIfFalse
public class TernaryExample {
    public static void main(String[] args) {
        int a = 10;
        String result = (a > 5) ? "Greater" : "Lesser";
        System.out.println(result); // Greater
    }
}

Type Comparison Operator (instanceof)

A binary operator that evaluates whether an object reference is an instance of a specific class, superclass, or interface, returning a boolean. As of Java 16, Pattern Matching for instanceof allows declaring a binding variable directly within the evaluation, eliminating the need for an explicit cast.
OperatorDescription
instanceofType comparison and optional pattern matching
public class InstanceofExample {
    public static void main(String[] args) {
        Object obj = "Java";
        
        // Standard instanceof
        System.out.println(obj instanceof String); // true
        
        // Pattern Matching for instanceof (Java 16+)
        if (obj instanceof String str) {
            System.out.println(str.toUpperCase()); // JAVA
        }
    }
}

Precedence and Associativity

When an expression contains multiple operators, precedence dictates the order of evaluation. Operators with higher precedence are evaluated before those with lower precedence. When operators of the same precedence appear in the same expression, associativity determines the evaluation direction:
  • Right-to-Left: Applies to prefix Unary, Ternary, and Assignment operators.
  • Left-to-Right: Applies to postfix Unary operators and all other binary operators.
Precedence Hierarchy (Highest to Lowest):
  1. Postfix (expr++, expr--)
  2. Unary (++expr, --expr, +expr, -expr, ~, !, (Type))
  3. Multiplicative (*, /, %)
  4. Additive (+, -)
  5. Shift (<<, >>, >>>)
  6. Relational (<, >, <=, >=, instanceof)
  7. Equality (==, !=)
  8. Bitwise AND (&)
  9. Bitwise XOR (^)
  10. Bitwise OR (|)
  11. Logical AND (&&)
  12. Logical OR (||)
  13. Ternary (? :)
  14. Assignment (=, +=, -=, etc.)
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More