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.

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.

Relational and Equality Operators

Evaluate the magnitude or equivalence relationship between two operands, strictly returning a boolean value.

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.

Bitwise and Bit Shift Operators

Operate directly on the binary representations of integer types (byte, short, int, long, char).

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.

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.

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.

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