🎯 What you will learn
Every program makes calculations and decisions. Java's operators are the glue between your variables and your logic. By the end of this lesson you will be able to write a working EMI calculator, a credit-score check, and a tax-bracket classifier — all using operators you learned here.
Six operator categories in this lesson:
Arithmetic · Comparison · Logical · Increment/Decrement · Compound Assignment · Ternary
i++ and ++i?" and "What does short-circuit evaluation mean?" appear in nearly every Java campus test. Both are covered here with verified examples.
➕ Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 100000 + 8500 | 108500 |
- | Subtraction | 50000 - 3500 | 46500 |
* | Multiplication | 100000 * 0.085 | 8500.0 |
/ | Division (integer if both operands are int) | 7 / 2 | 3 |
% | Modulo (remainder) | 108500 % 1000 | 500 |
Watch out for integer division: 7 / 2 gives 3, not 3.5. Add .0 to one operand (7.0 / 2) to get a double result.
🔍 Comparison & logical operators
Comparison operators return boolean. Logical operators (&&, ||, !) combine booleans. The critical feature is short-circuit evaluation: && stops at the first false; || stops at the first true.
Short-circuit magic: In the last line, creditScore < 300 is false, so Java never evaluates income / 0 — which would throw ArithmeticException. Short-circuit evaluation prevents the crash.
🧾 Real-world scenario
Meera is a backend engineer at TaxSetu in Bangalore. She needs to compute a user's net savings after tax, apply a 0.5% monthly interest, and decide their filing status using a ternary. She uses compound assignment operators to keep the balance-update code clean.
📐 Operator precedence (quick reference)
When multiple operators appear in one expression, Java evaluates them in this order (higher = first):
| Priority | Operators | Notes |
|---|---|---|
| 1 (highest) | ++ -- (post) | Post-increment/decrement |
| 2 | ! ++ -- (pre) | Unary operators |
| 3 | * / % | Multiplicative |
| 4 | + - | Additive |
| 5 | < > <= >= | Relational |
| 6 | == != | Equality |
| 7 | && | Logical AND |
| 8 | || | Logical OR |
| 9 (lowest) | = += -= *= … | Assignment |
When in doubt, use parentheses. (a + b) * c is always clearer than relying on precedence.
🧩 Mini challenge
Write a program that computes the EMI for a home loan. Formula: EMI = [P × r × (1+r)^n] / [(1+r)^n – 1] where P = ₹30,00,000, annual rate = 9%, n = 240 months. Use Math.pow(). Print the monthly EMI with 2 decimal places.
Given annual income = ₹8,50,000, compute tax using old Indian slab rates: 0–2.5L = 0%, 2.5–5L = 5%, 5–10L = 20%. Use arithmetic operators only (no if/else yet). Print the tax amount.
Write: int a = 5; int b = a++; int c = ++a;. Print a, b, and c. Explain why b and c differ even though both use ++.
🎤 Interview Q&A
3 — not 3.5. When both operands are int, Java performs integer division and discards the remainder. To get a floating-point result, make at least one operand a double: 7.0 / 2 → 3.5, or cast: (double) 7 / 2 → 3.5. This is a classic source of bugs in financial calculations — always verify that division involving money uses double or BigDecimal.
Both increment i by 1. The difference is the value returned in the expression. i++ (post-increment) returns the value before the increment. ++i (pre-increment) returns the value after the increment. Example: if i = 5, then int b = i++ sets b = 5 and leaves i = 6. int c = ++i increments first giving i = 7, then sets c = 7. In a standalone statement (i++;), both are identical.
&& (AND) short-circuits on false — if the left operand is false, the right is never evaluated. || (OR) short-circuits on true. This matters for two reasons: (1) safety — if (obj != null && obj.method()) avoids NullPointerException because obj.method() is only called when obj is not null; (2) performance — expensive operations (DB queries, API calls) can be placed on the right side so they are skipped when the left side determines the result.
The ternary operator condition ? valueIfTrue : valueIfFalse is a single-expression alternative to a two-branch if/else. Use it when you are assigning one of two values based on a condition: String label = (score >= 700) ? "APPROVED" : "REJECTED". Avoid nesting ternaries — a ? b ? c : d : e — it becomes unreadable fast. For complex branching, use if/else or switch expressions instead.
&& is the logical AND with short-circuit evaluation — it evaluates the right operand only if the left is true. & is the bitwise AND (and also the non-short-circuit logical AND for booleans) — it always evaluates both operands. For boolean expressions, you almost always want && because it is safer (avoids NPE) and faster. Use & when you explicitly need both sides evaluated regardless of the left result — rare in boolean logic, but common when working with bit flags in low-level code.
📝 Quiz — 5 questions
10 % 3 in Java?balance *= 1.005 mean?i = 5, what is the value of b after int b = i++?&&?Next: control flow with if/else and switch →
⚠️ Common mistakes
int tax = 85000 * 20 / 100; gives 17000 (correct by accident). But int rate = 1 / 4; gives 0, not 0.25. Always check whether division operands are int or double when fractional results are needed.if (score = 700) is a compile error in Java (unlike C/C++) — Java requires a boolean in if conditions. This is actually a helpful safety net — it prevents the classic "assignment vs comparison" bug.if (balance == 58792.50) with doubles. Binary floating point means the stored value may be 58792.4999999.... Use a tolerance: Math.abs(balance - 58792.50) < 0.01.if (a == 1 | b == 2) evaluates both sides always (bitwise OR on booleans is legal but uncommon). Use || for short-circuit logical OR. The difference matters when the right side has side-effects or can throw exceptions.✅ Key takeaways
7 / 2is3in Java (integer division) — use7.0 / 2or cast to get3.5.- The modulo operator
%gives the remainder — useful for checking even/odd, rounding to multiples, and cyclic indices. &&and||short-circuit — the right operand is skipped when the left operand determines the result.i++returns the value before incrementing;++ireturns the value after — matters inside expressions, not standalone statements.- Compound assignment (
+=,-=,*=) is cleaner and slightly more efficient than full assignment expressions. - Ternary
condition ? a : bis a concise single-value if/else — avoid nesting more than one level deep.
From operators to production Spring Boot
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →