Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An operator in Java is a special symbol that directs the compiler to perform specific mathematical, relational, logical, bitwise, or structural operations on one, two, or three operands, yielding a single result. Operators are fundamentally characterized by their arity (unary, binary, or ternary), precedence (evaluation order), and associativity (evaluation direction).

Arity Classification

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

Operator Categories and Syntax

1. Arithmetic Operators (Binary)

Perform standard mathematical computations on numeric types. The additive operator is overloaded to also perform String concatenation when at least one operand is a String.
  • + : Additive / String Concatenation
  • - : Subtraction
  • * : Multiplicative
  • / : Division (integer division truncates towards zero)
  • % : Modulo (remainder)
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
String concatenated = "Result: " + sum;

2. Unary Operators

Perform operations on a single operand, such as incrementing/decrementing a value, negating an expression, or inverting the value of a boolean.
  • + : Unary plus (performs unary numeric promotion, such as promoting a byte or short to an int; it does not change the sign of a negative value)
  • - : Unary minus (negates an expression)
  • ++ : Increment (adds 1)
  • -- : Decrement (subtracts 1)
  • ! : Logical complement (inverts boolean value)
Technical Note on Evaluation: Java evaluates expressions strictly left-to-right. For postfix operators, the variable is evaluated and incremented immediately, but the result of the postfix expression is the variable’s original value prior to the increment. It does not delay the increment until after the enclosing expression completes.
byte b = 5;
int promoted = +b; // Unary numeric promotion to int

int x = 5;
int prefixResult = ++x; // x becomes 6 immediately, prefixResult is 6

int y = 5;
int postfixResult = y++; // y becomes 6 immediately, but postfixResult is 5

boolean isFalse = !true;

3. Assignment Operators (Binary)

Bind the evaluated result of the right-hand expression to the left-hand variable. Assignment operators evaluate right-to-left.
  • = : Simple assignment
  • +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>= : Compound assignments.
Technical Note on Compound Assignment: According to JLS 15.26.2, a compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. This is a critical semantic rule for expressions with side effects. For example, in arr[i++] += 2, the index i is evaluated and incremented only once, whereas arr[i++] = arr[i++] + 2 would evaluate and increment i twice.
int variable = 10;
variable = 20;
variable *= 2 + 3; // Equivalent to: variable = (int) ((variable) * (2 + 3));

int[] arr = {1, 2, 3};
int i = 0;
arr[i++] += 5; // i is incremented only once. arr[0] becomes 6.

4. Relational Operators (Binary)

Evaluate the magnitude relationship between two operands, strictly returning a boolean (true or false).
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
int operand1 = 15;
int operand2 = 20;
boolean isGreaterOrEqual = operand1 >= operand2;

5. Equality Operators (Binary)

Evaluate whether two operands are equivalent or not equivalent, returning a boolean. According to the Java Language Specification, equality operators have a lower precedence than relational operators.
  • == : Equal to
  • != : Not equal to
int val1 = 5;
int val2 = 5;
boolean isEquivalent = (val1 == val2);

6. Logical Operators (Binary)

Perform boolean logic on boolean operands. These operators utilize short-circuit evaluation; the second operand is not evaluated if the first operand definitively determines the overall result.
  • && : Conditional-AND (returns true if both are true)
  • || : Conditional-OR (returns true if either is true)
boolean expr1 = true;
boolean expr2 = false;
boolean logicalAnd = expr1 && expr2;

7. Bitwise and Bit Shift Operators (Unary/Binary)

Perform operations on the individual bits of integer data types (byte, short, int, long, char). The &, |, and ^ operators can also be applied to boolean operands to function as non-short-circuiting logical operators. Applying the bitwise complement (~) or any bit shift operator (<<, >>, >>>) to a boolean operand results in a compilation error.
  • ~ : Unary bitwise complement (inverts bit pattern)
  • & : Bitwise AND
  • | : Bitwise inclusive OR
  • ^ : Bitwise exclusive OR
  • << : Signed left shift (shifts bits left, filling with zero)
  • >> : Signed right shift (shifts bits right, propagating the sign bit)
  • >>> : Unsigned right shift (shifts bits right, filling with zero regardless of sign)
int intOp1 = 0b1010;
int bitwiseComplement = ~intOp1;
int leftShift = intOp1 << 2;

boolean boolOp1 = true;
boolean boolOp2 = false;
boolean nonShortCircuitAnd = boolOp1 & boolOp2;

8. Ternary Operator

The only operator in Java that takes three operands. It acts as a condensed conditional expression.
  • ? : : Ternary conditional
boolean condition = true;
int result = condition ? 1 : 0;

9. Type Comparison Operator (Binary)

Compares an object reference to a specific reference type (class, subclass, or interface), returning a boolean.
  • instanceof : Type comparison
Object obj = "Hello";
boolean isString = obj instanceof String;

10. Lambda Operator (Binary)

Introduced in Java 8, the arrow operator separates the parameter list from the body of a lambda expression, enabling functional programming constructs.
  • -> : Lambda operator
java.util.function.IntUnaryOperator square = (int n) -> n * n;

Precedence and Associativity

When an expression contains multiple operators, Java uses precedence and associativity rules to determine the order of evaluation.
  • Precedence: Operators with higher precedence are evaluated before operators with lower precedence (e.g., * evaluates before +, and < evaluates before ==).
  • Associativity: Determines the evaluation order of operators with the same precedence.
    • Right-to-Left: Unary operators, Assignment operators, and the Ternary operator.
    • Left-to-Right: All other binary operators.
int op1 = 2;
int op2 = 3;
int op3 = 4;
int precedenceResult = (op1 + op2) * op3; // Parentheses override default precedence
Master Java with Deep Grasping Methodology!Learn More