Lesson 5 / 38
Operators
Work through Java's arithmetic, comparison and logical operators, short-circuit evaluation, and the pitfalls of integer division and silent overflow.
The common set
+ - * / % for arithmetic, == != < > <= >= for comparison, && || ! for boolean logic (short-circuiting), and +=, ++ style compound assignment.
int a = 7, b = 2;
System.out.println(a / b); // 3 (integer division!)
System.out.println(a % b); // 1
System.out.println(a / 2.0); // 3.5 (one double operand)Integer overflow is silent
int is 32-bit and wraps around past ~2.1 billion with no error. Use long for large counts, or Math.addExact(...) to get an exception on overflow.