Java FundamentalsLesson 1.3
Java operators and expression evaluation order
arithmetic operators, comparison operators, logical operators, assignment operators, operator precedence, short-circuit evaluation, integer division
Operators and Precedence
Operators control how values combine. Precedence controls which operation runs first when you mix them.
Arithmetic
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3 โ integer division, remainder dropped
System.out.println(a % b); // 1 โ modulus (remainder)
Integer division truncates โ 10 / 3 is 3, not 3.33. Cast to double first: (double) a / b.
Comparison and Logical
boolean result = (a > 5) && (b < 5); // true
boolean either = (a > 20) || (b < 5); // true
boolean not = !(a == 10); // false
Short-circuit evaluation: In &&, if the left side is false, the right side never evaluates. In ||, if the left is true, the right is skipped. This matters when the right side has a side effect or null check.
Compound Assignment
int x = 5;
x += 3; // x = 8
x *= 2; // x = 16
x++; // x = 17 (post-increment)
++x; // x = 18 (pre-increment)
Use parentheses to override default precedence whenever the order is non-obvious. Explicit grouping prevents bugs and aids readability.
