Java Operators: Arithmetic, Comparison, Logic, Bits and Precedence
Java operators act on one, two, or three operands and produce a result.
Arithmetic operators
Unary ++ and -- increment or decrement a variable. Prefix form changes it before yielding a value; postfix form yields the previous value first.
Assignment operators
= assigns a value. Compound forms combine an operation with assignment:
Comparison operators
==, !=, <, >, <=, and >= return a Boolean result.
For objects, == compares references. Use .equals() when the class defines value equality:
| |
Logical operators
&&: logical AND with short-circuiting||: logical OR with short-circuiting!: logical negation
| |
Bitwise and shift operators
Integral types support &, |, ^, and ~, plus <<, >>, and >>> shifts:
>> preserves the sign bit, while >>> shifts in zeros.
Conditional operator
The ternary operator selects one of two values:
| |
Parentheses are helpful when precedence is not immediately obvious. Prefer readable expressions over relying on memorized precedence rules.