Java operators act on one, two, or three operands and produce a result.
Arithmetic operators
1 | int a = 10; |
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:
1 | int total = 10; |
Comparison operators
==, !=, <, >, <=, and >= return a Boolean result.
1 | boolean sameNumber = a == b; |
For objects, == compares references. Use .equals() when the class defines value equality:
1 | boolean sameText = firstString.equals(secondString); |
Logical operators
&&: logical AND with short-circuiting||: logical OR with short-circuiting!: logical negation
1 | boolean canEnter = hasTicket && !isBanned; |
Bitwise and shift operators
Integral types support &, |, ^, and ~, plus <<, >>, and >>> shifts:
1 | int flags = 0b0101; |
>> preserves the sign bit, while >>> shifts in zeros.
Conditional operator
The ternary operator selects one of two values:
1 | String result = score >= 60 ? "Pass" : "Fail"; |
Parentheses are helpful when precedence is not immediately obvious. Prefer readable expressions over relying on memorized precedence rules.