Java Essentials · Ay 1
Operators & Expressions
Goal: calculate and compare values with Java operators and use the
Mathclass. Duration: 90 min
1. Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
add | 7 + 2 |
9 |
- |
subtract | 7 - 2 |
5 |
* |
multiply | 7 * 2 |
14 |
/ |
divide | 7 / 2 |
3 (int!) |
% |
remainder (modulo) | 7 % 2 |
1 |
% is very useful:
boolean isEven = number % 2 == 0;
int lastDigit = number % 10;
int minutes = totalSeconds / 60, seconds = totalSeconds % 60;
2. Assignment and increment
int x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // 12
x *= 2; // 24
x /= 4; // 6
x %= 4; // 2
int i = 5;
int a = i++; // a = 5, then i = 6 (post-increment)
int b = ++i; // i = 7, then b = 7 (pre-increment)
Rule: in real code use i++ alone on a line; avoid mixing it inside expressions.
3. Comparison operators → boolean
==, !=, >, <, >=, <=
int balance = 500;
boolean canPay = balance >= 300; // true
⚠️ Never compare
Strings with==. Use.equals()(lesson 4).
4. Logical operators
| Operator | Meaning | True when |
|---|---|---|
&& |
AND | both are true |
|| |
OR | at least one is true |
! |
NOT | the value is false |
boolean adult = age >= 18;
boolean hasIncome = salary > 0;
boolean canGetLoan = adult && hasIncome && !blacklisted;
Short-circuit: in a && b, if a is false, b is not evaluated. This protects us:
if (count != 0 && total / count > 10) { ... } // no division by zero
5. Precedence
() → ++ -- ! → * / % → + - → comparisons → && → || → =
int r = 2 + 3 * 4; // 14
int s = (2 + 3) * 4; // 20
When in doubt, use parentheses: they make code readable.
6. The Math class
Math.max(4, 9) // 9
Math.min(4, 9) // 4
Math.abs(-7) // 7
Math.pow(2, 10) // 1024.0
Math.sqrt(144) // 12.0
Math.round(4.6) // 5
Math.floor(4.9) // 4.0
Math.ceil(4.1) // 5.0
Math.random() // 0.0 ≤ x < 1.0
int dice = (int) (Math.random() * 6) + 1; // 1..6
7. Step by step: leap year and BMI
public class Calculations {
public static void main(String[] args) {
int year = 2028;
boolean leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
System.out.println(year + " is leap: " + leap);
double weightKg = 78, heightM = 1.80;
double bmi = weightKg / Math.pow(heightM, 2);
System.out.println("BMI: " + Math.round(bmi * 10) / 10.0); // 24.1
}
}
8. Common mistakes
- ❌
=(assignment) instead of==(comparison). - ❌
5 / 2 * 2.0→4.0, not5.0(integer division happens first). - ❌ Using
&/|instead of&&/||(no short-circuit).
9. Exercises
- Given a 3-digit number, print the sum of its digits (
472 → 13). - Check if a number is between 10 and 50 (inclusive).
- Convert 7384 seconds to
hh:mm:ss. - Calculate the monthly loan payment:
P * r / (1 - Math.pow(1 + r, -n))for P = 10000, annual 12%, 24 months.
10. Homework
ATM logic without if: given amount = 1780, print how many 200, 100, 50, 20, 10 AZN notes are needed (use / and %).
11. Self-check
- What is
17 % 5? - What is the value of
xafterint x = 3; x += x * 2;? - Why is
&&safer than&incount != 0 && total / count > 1?
Answers
29- Short-circuit: if
countis 0, the division is never executed.