CKTIS / Java
Expressions and operators in Java
Expressions and operators in Java: Java expressions evaluate left to right under fixed precedence, numeric promotion, and arithmetic, logical, bitwise, comparison, and assignment operators.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
expressions operatorsJava expressions evaluate left to right under fixed precedence, numeric promotion, and arithmetic, logical, bitwise, comparison, and assignment operators.
Use parentheses for intent, && and || for conditions, Objects.equals for objects, and Math.addExact or wider types when overflow must be detected.
== on references tests identity rather than content; integer division truncates, int/long overflow wraps, and + with a String turns the rest into concatenation.
Shift distances are masked to 5 or 6 bits, integer division by zero throws ArithmeticException, and ?: can apply target typing and unbox null into NPE.
The five lines expose && short-circuiting without increment, int overflow, String concatenation direction, and masked shift distance.
Examples
public class Main {
public static void main(String[] args) {
int x = 1;
boolean ignored = false && ++x > 1;
System.out.println(x);
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(1 + 2 + "x");
System.out.println("x" + 1 + 2);
System.out.println(33 << 32);
}
}
Result
1 -2147483648 3x x12 33
The five lines expose && short-circuiting without increment, int overflow, String concatenation direction, and masked shift distance.