★ India’s Student Guidance Platform

Operators and Expressions in Java — ICSE Class 10 Computer Applications Notes

Operators and Expressions in Java — ICSE Class 10 Computer Applications Notes

Agar Java ek bhasha hai, toh operators uske verbs hain — the words that actually do things. Every ICSE Computer Applications paper leans on them: Section A ke output-prediction questions se lekar Section B ke programs tak, operators har jagah hain. Good news? There are only a handful of them, and once you know three-four golden rules, the trickiest questions become free marks. Chaliye, aaram se, ek-ek karke.

🎯 Try This
Take two numbers from your daily life (like your age and a friend’s age) and manually work out the result of five different operators (+, -, *, /, %) applied to them, then check your answers on paper. (15 min)

Arithmetic Operators

Paanch operators: + (add), - (subtract), * (multiply), / (divide), % (modulus — remainder). Chaar toh school-maths hain; asli kahani / aur % mein hai.

Key Rule — Integer Division
If BOTH operands are integers, / throws away the decimal part (no rounding — truncation). If EITHER side is a double, you get a decimal answer.
Example 1 — int vs double division
System.out.println(7 / 2);
System.out.println(7.0 / 2);

Output:

3
3.5

7/2 mein dono int hain → 3.5 nahi, sirf 3 (decimal cut). 7.0 ne poore expression ko double bana diya → 3.5.

Example 2 — Modulus — the remainder operator
System.out.println(7 % 2);
System.out.println(-7 % 2);
System.out.println(7 % -2);

Output:

1
-1
1

Yaad rakhne ka easy tareeka: result ka sign LEFT operand ka sign hota hai. −7%2 → −1, par 7%−2 → 1.

Common Mistake
Writing 19/4 = 4.75 in an output question. Dono operands int hain, isliye answer 4 hai. Yeh ICSE ka sabse favourite trap hai — har saal aata hai.

↑ Back to top

Assignment & Shorthand Operators

= assignment hai (comparison nahi!). Shorthand forms kaam chhota kar deti hain: a += 5 ka matlab a = a + 5. Same for -=, *=, /=, %=.

Example 3 — Shorthand ka hidden bracket
int a = 8;
a *= 2 + 3;
System.out.println(a);

Output:

40

Dhyan se: a *= 2+3 means a = a * (2+3) = 8×5 = 40, not 8×2+3 = 19. Shorthand operator poore right side par lagta hai — jaise ek invisible bracket ho.

Exam Tip
Shorthand operators (jaise b += 5 on a byte) automatically type-cast bhi karte hain — b = b + 5 byte ke liye error deta par b += 5 chal jaata hai. 1-mark theory question ke liye ready raho.

↑ Back to top

Increment and Decrement (++ / −−)

Yeh unary operators variable ko 1 se badhate/ghatate hain. Asli khel PRE vs POST ka hai:

  • ++x (pre): pehle badhao, phir value use karo
  • x++ (post): pehle value use karo, phir badhao
Example 4 — The classic pre/post dry run
int x = 5;
int y = x++ + ++x;
System.out.println(x + " " + y);

Output:

7 12

Step-by-step: x++ deta hai 5 (phir x=6). ++x pehle x=7 karta hai, deta hai 7. So y = 5+7 = 12, x = 7.

Example 5 — One more — mixed order
int a = 2;
int r = a++ * 3 + --a;
System.out.println(a + " " + r);

Output:

2 8

a++*3 → 2×3=6 (a becomes 3). --a → a wapas 2, deta hai 2. r = 6+2 = 8, a = 2.

Common Mistake
Left-to-right chalna kabhi mat bhoolo. Har ++/-- ke baad variable ki nayi value ek margin mein likhte jao — ICSE examiner isi habit ko full marks deta hai.

↑ Back to top

Relational Operators

Comparison ke liye: >, <, >=, <=, == (equal), != (not equal). Result hamesha boolean hota hai — true ya false, bas.

Key Rule
= assigns, == compares. if(a = 5) Java mein compile hi nahi hota (booleans ke alawa) — aur theory mein iska difference likhna 1 easy mark hai.

↑ Back to top

Logical Operators & Short-Circuiting

Conditions jodne ke liye: && (AND — dono true), || (OR — koi ek true), ! (NOT — ulta). Inka secret weapon: short-circuit evaluation.

Example 6 — Truth in action
System.out.println(5 > 3 && 2 > 4);
System.out.println(5 > 3 || 2 > 4);
System.out.println(!(5 > 3));

Output:

false
true
false
Example 7 — Short-circuit proof
boolean flag = false; int cnt = 0;
if (flag && (++cnt > 0)) { }
System.out.println(cnt);

Output:

0

Kyunki flag false tha, Java ne ++cnt ko dekha tak nahi — AND ka left side false ho toh right side skip. Isliye cnt 0 hi raha. Same trick || ke saath: left true → right skip.

↑ Back to top

The Ternary Operator

Ek line ka if-else: condition ? valueIfTrue : valueIfFalse. Java ka single operator jo TEEN operands leta hai.

Example 8 — Bigger of two
int big = 17, sm = 8;
System.out.println(big > sm ? big : sm);

Output:

17
Example 9 — Even or Odd in one line
int n = 7;
System.out.println((n % 2 == 0) ? "Even" : "Odd");

Output:

Odd
Exam Tip
Section B ke programs mein ternary use karke lines bachao — examiner ko clean logic pasand hai. Par nested ternary se bacho, readability first.

↑ Back to top

Precedence & Associativity

Ek expression mein kaun pehle chalega? Simplified ladder (top = first): ++/--/!* / %+ - → relational → &&|| → ternary → =. Same level par (jaise * aur /) → left to right (associativity).

Example 10 — Ladder in action
System.out.println(10 + 3 * 2);
System.out.println((10 + 3) * 2);
System.out.println(8 / 2 * 4);
System.out.println(10 - 4 + 2);

Output:

16
26
16
8

8/2*4: division pehle nahi — dono same level hain, left-to-right → 4×4=16. 10-4+2 → 6+2=8, not 4.

Common Mistake
8/2*4 ko 8/(2×4)=1 padhna — galat! Brackets ke bina Java kabhi right side pehle nahi karta jab operators same level ke hon.

↑ Back to top

Type Conversion in Expressions

Mixed-type expression mein chhota type automatically bade mein promote ho jaata hai (implicit conversion): byte→short→int→long→float→double, aur char→int. Zabardasti karne ke liye cast lagao: (int)7.9.

Example 11 — char ki asli value
System.out.println('A' + 1);
System.out.println((char)('A' + 1));
System.out.println((int)'a');

Output:

66
B
97

'A' ka Unicode 65 hai. ‘A’+1 arithmetic hai → 66. Wapas letter chahiye toh cast karo → ‘B’. Small ‘a’ = 97 — yeh teen values (65, 97, 48 for ‘0’) yaad kar lo, ICSE mein har doosre saal aati hain.

Example 12 — Casting vs promotion
double d = 5;
System.out.println(d / 2);
int i = (int)7.9;
System.out.println(i);
System.out.println(9 / 2.0);

Output:

2.5
7
4.5

(int)7.9 rounds nahi karta — truncate karta hai → 7.

↑ Back to top

Dry-Run Bank — Output Questions

ICSE Section A ka signature move: “What will be the output?” Neeche ke har snippet ko pehle KHUD solve karo, phir answer kholo.

Dry Run 1
int k = 10;
System.out.println(k++ + k++);
System.out.println(k);
Show Answer

Output:

21
12

k++ → 10 (k=11), k++ → 11 (k=12). Sum 21, phir k=12.

Dry Run 2
int p = 5;
int q = ++p + p++;
System.out.println(p + " " + q);
Show Answer

Output:

7 12

++p → 6, p++ → 6 (phir 7). q=12, p=7.

Dry Run 3
System.out.println(100 / 8 * 8 + 100 % 8);
Show Answer

Output:

100

100/8=12 → 12×8=96 → +4 = 100. (Yeh identity har number ke liye chalti hai!)

Dry Run 4
int x = 4;
int y = x-- - --x;
System.out.println(x + " " + y);
Show Answer

Output:

2 2

x– → 4 (x=3), –x → 2. y=4−2=2, x=2.

↑ Back to top

Practice Worksheet

Q1. Predict the output:
System.out.println(19/4); and System.out.println(19.0/4);
Show Answer

4 aur 4.75 — pehla integer division (truncation), doosre mein 19.0 ne expression ko double bana diya.

Q2. Evaluate: 17 % 5 and -17 % 5
Show Answer

2 aur −2 — modulus ka sign left operand se aata hai.

Q3. int a=8; a *= 2+3; — What is a?
Show Answer

40 — shorthand means a = a × (2+3), poora right side pehle.

Q4. Predict: int x=4; int y = x-- - --x; — x and y?
Show Answer

x=2, y=2 — x– deta 4 (x=3), –x deta 2, y=4−2=2.

Q5. System.out.println('Z'+1); and System.out.println((char)('Z'+1));
Show Answer

91 aur [ — ‘Z’=90; +1 arithmetic; cast se character milta hai (91 = ‘[‘).

Q6. True or false: 5+2>6 && 3<1
Show Answer

false — 7>6 true, par 3<1 false; AND needs both.

Q7. Write a ternary statement to print “Pass” if marks ≥ 33, else “Fail”.
Show Answer

System.out.println(marks >= 33 ? "Pass" : "Fail");

Q8. Why does b += 5 compile for a byte b, but b = b + 5 does not? (1-mark theory)
Show Answer

Kyunki shorthand operators result ko automatically left side ke type mein cast kar dete hain; b+5 int ban jaata hai jo byte mein bina cast ke nahi ja sakta.

Aage kya? Isi foundation par conditionals khade hote hain — pehle Java Basics & OOP Concepts aur Values & Data Types revise kar lo agar operators ke types mein confusion ho. Poora section: ICSE Class 10 hub.

Kaizen: Roz sirf 3 dry-run questions khud se solve karo — do hafte mein output questions aapke liye free marks ban jayenge. 小さな改善, har din. 🌱

Written & reviewed by Team Principal Saab — Meet the team →