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).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.
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 aString.
+: Additive / String Concatenation-: Subtraction*: Multiplicative/: Division (integer division truncates towards zero)%: Modulo (remainder)
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 abyteorshortto anint; 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)
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.
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.
4. Relational Operators (Binary)
Evaluate the magnitude relationship between two operands, strictly returning aboolean (true or false).
>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
5. Equality Operators (Binary)
Evaluate whether two operands are equivalent or not equivalent, returning aboolean. According to the Java Language Specification, equality operators have a lower precedence than relational operators.
==: Equal to!=: Not equal to
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 (returnstrueif both aretrue)||: Conditional-OR (returnstrueif either istrue)
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)
8. Ternary Operator
The only operator in Java that takes three operands. It acts as a condensed conditional expression.? :: Ternary conditional
9. Type Comparison Operator (Binary)
Compares an object reference to a specific reference type (class, subclass, or interface), returning aboolean.
instanceof: Type comparison
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
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.
Master Java with Deep Grasping Methodology!Learn More





