🎯 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

💼 Interview reality: "What is the difference between 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

OperatorMeaningExampleResult
+Addition100000 + 8500108500
-Subtraction50000 - 350046500
*Multiplication100000 * 0.0858500.0
/Division (integer if both operands are int)7 / 23
%Modulo (remainder)108500 % 1000500

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.

javac ArithmeticOps.java && java ArithmeticOps
ArithmeticOps.javaLesson 02 · interest calculation
verified output
Principal : ₹100000 Interest : ₹8500.00 Total : ₹108500 Remainder : ₹500 Integer ÷ : 108 complete thousands

🔍 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.

javac ComparisonLogical.java && java ComparisonLogical
ComparisonLogical.javaLesson 02 · loan approval logic
verified output
creditScore >= 700 : true creditScore == 800 : false income > 40000 : true Loan approved : true Short-circuit && : false

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

🧾
Real-world scenario · TaxSetu
Meera computes a user's monthly savings and tax refund status

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.

javac IncrementTernary.java && java IncrementTernary
IncrementTernary.javaTaxSetu · compound assignment & ternary
verified output
Transactions processed: 3 Balance after ops : ₹58792.50 Application status : APPROVED

📐 Operator precedence (quick reference)

When multiple operators appear in one expression, Java evaluates them in this order (higher = first):

PriorityOperatorsNotes
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

Three operator exercises
Task 1 — EMI calculator

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.

Task 2 — Tax bracket

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.

Task 3 — Post vs pre-increment

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

EasyWhat is the result of 7 / 2 in Java?
+

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.

EasyWhat is the difference between i++ and ++i?
+

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.

MediumWhat is short-circuit evaluation and why does it matter?
+

&& (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.

MediumWhat is the ternary operator and when should you use it?
+

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.

HardExplain the difference between & and && in Java.
+

&& 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

Question 1 of 5
What is the result of 10 % 3 in Java?
Question 2 of 5
Which operator checks equality of two primitive values?
Question 3 of 5
What does balance *= 1.005 mean?
Question 4 of 5
If i = 5, what is the value of b after int b = i++?
Question 5 of 5
What does short-circuit evaluation mean for &&?
0
/ 5
Quiz complete!

Next: control flow with if/else and switch →

⚠️ Common mistakes

Integer division surprises
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.
Assignment in conditions
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.
Floating-point equality
Never write 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.
Using | instead of ||
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 / 2 is 3 in Java (integer division) — use 7.0 / 2 or cast to get 3.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; ++i returns the value after — matters inside expressions, not standalone statements.
  • Compound assignment (+=, -=, *=) is cleaner and slightly more efficient than full assignment expressions.
  • Ternary condition ? a : b is a concise single-value if/else — avoid nesting more than one level deep.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →