Arithmetic and Boolean Expressions
Arithmetic Operators
Java provides the standard arithmetic operators for numeric types:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 3 + 4 → 7 |
- |
Subtraction | 10 - 3 → 7 |
* |
Multiplication | 3 * 4 → 12 |
/ |
Division | 10 / 3 → 3 (integer division) |
% |
Modulo (remainder) | 10 % 3 → 1 |
Integer Division and Modulo
Integer division truncates toward zero — the fractional part is discarded:
javaint a = 10 / 3; // 3 (not 3.33)
int b = -10 / 3; // -3 (truncates toward zero)
int c = 10 % 3; // 1
int d = -10 % 3; // -1 (sign follows the dividend)Dividing an integer by zero throws ArithmeticException. Floating-point division by zero yields Infinity or NaN:
javaint bad = 5 / 0; // ArithmeticException
double inf = 5.0 / 0; // Infinity
double nan = 0.0 / 0; // NaNUnary Operators
| Operator | Description | Example |
|---|---|---|
+ |
Unary plus (rarely used) | +x |
- |
Unary minus (negation) | -x |
++ |
Increment by 1 | x++ / ++x |
-- |
Decrement by 1 | x-- / --x |
Pre- vs Post-increment
The prefix form increments before the value is used; the postfix form increments after:
javaint x = 5;
System.out.println(x++); // prints 5, then x becomes 6
System.out.println(++x); // x becomes 7, then prints 7
int y = 5;
int a = y++; // a = 5, y = 6
int b = ++y; // y = 7, b = 7Exam tip:
x++returns the old value;++xreturns the new value. These appear frequently in loop conditions and assignment expressions on the exam.
Compound Assignment Operators
Compound operators combine an arithmetic operation with assignment:
javaint x = 10;
x += 3; // x = 13
x -= 2; // x = 11
x *= 2; // x = 22
x /= 4; // x = 5
x %= 3; // x = 2Compound assignment operators include an implicit cast, so this compiles even though it would not with an explicit operation:
javabyte b = 5;
b += 3; // OK — implicit cast: equivalent to b = (byte)(b + 3)
b = b + 3; // COMPILE ERROR — b + 3 promotes to intNumeric Promotion Rules
When operands of different types are combined, smaller types are promoted before the operation:
byte,short, andcharare promoted tointbefore any binary operation.- If one operand is
long, the other is promoted tolong. - If one operand is
float, the other is promoted tofloat. - If one operand is
double, the other is promoted todouble.
javabyte b = 10;
short s = 20;
// b + s is int, not byte or short
int result = b + s; // OK
byte bad = b + s; // COMPILE ERROR — int cannot be assigned to byteBoolean Expressions
A boolean expression evaluates to true or false and is used in conditions.
Relational Operators
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
javaint x = 5;
boolean a = x == 5; // true
boolean b = x != 5; // false
boolean c = x > 3; // true
boolean d = x <= 5; // trueLogical Operators
| Operator | Type | Description |
|---|---|---|
& |
AND (eager) | Both sides always evaluated |
| |
OR (eager) | Both sides always evaluated |
^ |
XOR | True when exactly one side is true |
! |
NOT | Inverts the boolean value |
&& |
AND (short-circuit) | Right side skipped if left is false |
|| |
OR (short-circuit) | Right side skipped if left is true |
javaint x = 5;
// Short-circuit: right side not evaluated when not needed
boolean a = (x > 10) && (x++ > 0); // false; x is still 5
boolean b = (x > 0) || (x++ > 0); // true; x is still 5
// Eager: both sides always evaluated
boolean c = (x > 10) & (x++ > 0); // false; x becomes 6
boolean d = (x > 0) | (x++ > 0); // true; x becomes 7Exam tip: Use
&&and||when the right-hand side has side effects (likex++). Short-circuit evaluation can preventNullPointerExceptionwhen checking for null before dereferencing.
Short-circuit Evaluation for Null Safety
javaString s = null;
// Without short-circuit — NullPointerException:
if (s != null & s.length() > 0) { } // NPE when s is null
// With short-circuit — safe:
if (s != null && s.length() > 0) { } // OK — right side skipped when s is nullString Concatenation with +
When + has a String operand, it performs concatenation. Order of evaluation matters:
javaSystem.out.println(1 + 2 + "3"); // "33" (1+2=3, then "3"+"3")
System.out.println("1" + 2 + 3); // "123" ("1"+"2"="12", then "12"+"3")
System.out.println("1" + (2 + 3)); // "15" (parentheses force numeric addition first)Key Points for the Exam
- Integer division truncates toward zero; integer division by zero throws
ArithmeticException. x++returns the original value;++xreturns the incremented value.- Compound assignments (
+=,-=, etc.) include an implicit narrowing cast. byte,short, andcharare promoted tointin binary expressions.&&and||short-circuit;&and|always evaluate both operands.+with aStringoperand performs concatenation, not addition.