π Operators Kya Hain?
int result = 10 + 5; β β β operand op operand
Operator = Symbol jo operation karta hai (+, -, >, &&)
Operand = Jis value par operation hota hai (10, 5)
Deep Explanation:
+ β Addition - β Subtraction * β Multiplication / β Division (int/int = int, decimal cut!) % β Modulus (remainder - bacha hua)
Important: % (modulus) real life mein bohot use hota hai:
10 % 3 = 1 β 10 ko 3 se divide karo, remainder 1 Even/Odd check: n % 2 == 0 β even
public class Arithmetic1 { public static void main(String[] args) { int a = 17, b = 5; System.out.println("a + b = " + (a + b)); // 22 System.out.println("a - b = " + (a - b)); // 12 System.out.println("a * b = " + (a * b)); // 85 System.out.println("a / b = " + (a / b)); // 3 (not 3.4!) System.out.println("a % b = " + (a % b)); // 2 (remainder) // int division vs double division double div = (double) a / b; System.out.println("exact division = " + div); // 3.4 } }
17/5 = 3 kyunki int/int mein decimal cut hota hai. 17%5 = 2 kyunki 17 = 5Γ3 + 2.public class Arithmetic2 { public static void main(String[] args) { // Modulus ke real uses int n1 = 28; int n2 = 33; // Even ya Odd check if (n1 % 2 == 0) System.out.println(n1 + " is Even"); else System.out.println(n1 + " is Odd"); // Divisibility check if (n2 % 3 == 0) System.out.println(n2 + " is divisible by 3"); else System.out.println(n2 + " is NOT divisible by 3"); // Last digit nikalna int num = 12345; System.out.println("Last digit: " + num % 10); // 5 // Clock/Circular problem int hour = 14 % 12; System.out.println("14:00 = " + hour + " PM"); // 2 PM } }
% ka real use β even/odd, divisibility, last digit, aur circular (clock) problems mein.public class Arithmetic3 { public static void main(String[] args) { // String mein + concatenation karta hai int a = 10, b = 20; System.out.println("Sum = " + a + b); // "Sum = 1020" β wrong! System.out.println("Sum = " + (a + b)); // "Sum = 30" β sahi // Explanation: // "Sum = " + 10 = "Sum = 10" (string) // "Sum = 10" + 20 = "Sum = 1020" (concatenation!) // () use karo toh pehle 10+20=30 hoga String name = "Rahul"; int age = 20; System.out.println("Name: " + name + ", Age: " + age); } }
+ string ke saath concatenation karta hai. "" + 10 + 20 = "1020" β isliye numbers ke around () lagao.public class Arithmetic4 { public static void main(String[] args) { // Real world - Bill calculator int items = 5; double pricePerItem = 99.50; double total = items * pricePerItem; double tax = total * 0.18; // 18% GST double finalBill = total + tax; System.out.println("Items: " + items); System.out.println("Price each: Rs." + pricePerItem); System.out.println("Total: Rs." + total); System.out.println("GST (18%): Rs." + tax); System.out.println("Final Bill: Rs." + finalBill); // Remainder use - pizza slices int totalSlices = 23; int people = 4; System.out.println("\nEach person gets: " + totalSlices / people + " slices"); System.out.println("Leftover slices: " + totalSlices % people); } }
Deep Explanation:
= β simple assign a = 10 += β add and assign a += 5 means a = a + 5 -= β subtract assign a -= 5 means a = a - 5 *= β multiply assign a *= 5 means a = a * 5 /= β divide assign a /= 5 means a = a / 5 %= β modulus assign a %= 5 means a = a % 5
Shortcut operators hain β code short likhne ke liye.
public class Assignment1 { public static void main(String[] args) { int a = 20; System.out.println("Initial a = " + a); a += 5; // a = a + 5 = 25 System.out.println("After a += 5 β " + a); a -= 3; // a = a - 3 = 22 System.out.println("After a -= 3 β " + a); a *= 2; // a = a * 2 = 44 System.out.println("After a *= 2 β " + a); a /= 4; // a = a / 4 = 11 System.out.println("After a /= 4 β " + a); a %= 3; // a = a % 3 = 2 System.out.println("After a %= 3 β " + a); } }
public class Assignment2 { public static void main(String[] args) { // Real use - score system int score = 0; System.out.println("Start Score: " + score); score += 10; // level complete System.out.println("Level 1 done: " + score); score += 25; // bonus System.out.println("Bonus earned: " + score); score -= 5; // penalty System.out.println("Penalty applied: " + score); score *= 2; // double points event System.out.println("Double points: " + score); // Shopping cart double cart = 0; cart += 299.0; // shirt cart += 499.0; // jeans cart -= 50.0; // coupon discount System.out.println("\nCart Total: Rs." + cart); } }
Deep Explanation:
== β equal to (10 == 10 β true) != β not equal to (10 != 5 β true) > β greater than (10 > 5 β true) < β less than (5 < 10 β true) >= β greater or equal (10 >= 10 β true) <= β less or equal (5 <= 10 β true)
Result hamesha boolean hota hai β true ya false
β οΈ Common Mistake:
= β assignment (value daalna) a = 5 == β comparison (value check) a == 5
public class Comparison1 { public static void main(String[] args) { int a = 15, b = 10; System.out.println("a == b : " + (a == b)); // false System.out.println("a != b : " + (a != b)); // true System.out.println("a > b : " + (a > b)); // true System.out.println("a < b : " + (a < b)); // false System.out.println("a >= b : " + (a >= b)); // true System.out.println("a <= b : " + (a <= b)); // false // Comparison result boolean mein store kar sakte hain boolean isEqual = (a == b); boolean isGreater = (a > b); System.out.println("isEqual: " + isEqual); System.out.println("isGreater: " + isGreater); } }
public class Comparison2 { public static void main(String[] args) { // Real use - eligibility checks int age = 20; int marks = 85; double salary = 25000.0; // Voting System.out.println("Can vote (>=18): " + (age >= 18)); // Scholarship System.out.println("Scholarship (>80): " + (marks > 80)); // Tax System.out.println("Pays tax (>20000): " + (salary > 20000)); // Grade String grade; if (marks >= 90) grade = "A+"; else if (marks >= 75) grade = "A"; else if (marks >= 60) grade = "B"; else grade = "C"; System.out.println("Grade: " + grade); } }
Deep Explanation:
&& β AND β dono true hone chahiye || β OR β ek bhi true ho toh true ! β NOT β ulta kar do (trueβfalse, falseβtrue)
Truth Table:
public class Logical1 { public static void main(String[] args) { boolean a = true, b = false; System.out.println("a && b = " + (a && b)); // false System.out.println("a || b = " + (a || b)); // true System.out.println("!a = " + (!a)); // false System.out.println("!b = " + (!b)); // true System.out.println("true && true = " + (true && true)); System.out.println("true && false = " + (true && false)); System.out.println("false || true = " + (false || true)); System.out.println("false || false = " + (false || false)); } }
public class Logical2 { public static void main(String[] args) { int age = 22; int marks = 75; boolean hasId = true; boolean isMember = false; // AND - dono conditions true honi chahiye if (age >= 18 && marks >= 60) { System.out.println("Eligible for admission"); } // OR - ek bhi true ho if (hasId || isMember) { System.out.println("Entry allowed"); } // NOT - condition ulti karo if (!isMember) { System.out.println("Not a member - pay Rs.100 extra"); } // Combined if (age >= 18 && marks >= 60 && hasId) { System.out.println("All conditions met!"); } } }
Deep Explanation β Interview mein zaroor aata hai!
Short-Circuit matlab: Agar pehli condition se hi answer pata chal jaaye toh dusri condition evaluate hi nahi hoti!
&& (Short-Circuit AND): pehli condition FALSE β dusri check hi nahi karega β result FALSE pehli condition TRUE β dusri check karega || (Short-Circuit OR): pehli condition TRUE β dusri check hi nahi karega β result TRUE pehli condition FALSE β dusri check karega
Real Example:
int x = 0; if (x != 0 && 10/x > 1) { } β x=0 hai toh pehli condition false 10/0 division by zero hoga - BUT evaluate hi nahi hoga! Short circuit ne bachaya!
public class ShortCircuit1 { public static void main(String[] args) { int x = 0; // Without short-circuit (& single) - DONO evaluate hote hain // if (x != 0 & 10/x > 1) // β ArithmeticException aayega! // With short-circuit (&& double) - SAFE! if (x != 0 && 10 / x > 1) { System.out.println("Condition true"); } else { System.out.println("Short-circuit saved us! x=0 caught first"); } // || short circuit int a = 5; if (a > 0 || 10 / 0 > 1) { // 10/0 evaluate hi nahi hoga! System.out.println("OR short-circuit: first was true, skipped second"); } } }
&& β pehli false toh ruk jao. || β pehli true toh ruk jao. Dusri condition ka code execute hi nahi hota!public class ShortCircuit2 { static int checkA() { System.out.println("checkA() called"); return 5; } static int checkB() { System.out.println("checkB() called"); return 10; } public static void main(String[] args) { System.out.println("--- && Test ---"); // pehli condition false hai if (checkA() > 10 && checkB() > 5) { // checkA()=5, 5>10=false System.out.println("Both true"); } else { System.out.println("Result: false"); } // checkB() called hi nahi hoga! System.out.println("\n--- || Test ---"); // pehli condition true hai if (checkA() > 0 || checkB() > 5) { // checkA()=5, 5>0=true System.out.println("Result: true"); } // checkB() called hi nahi hoga! } }
checkB() ka print nahi aaya β matlab wo method call hi nahi hua! Yahi short-circuit proof hai.public class ShortCircuit3 { public static void main(String[] args) { // Null check - real world use String name = null; // Without short-circuit - NullPointerException aayega! // if (name.length() > 0 && name.equals("Rahul")) { } // With short-circuit - SAFE! if (name != null && name.length() > 0) { System.out.println("Name is: " + name); } else { System.out.println("Name is null or empty - safely handled!"); } // Single & vs && difference int count = 0; // & evaluates BOTH sides always // boolean r1 = (count != 0) & (10 / count > 1); // CRASH! // && evaluates second only if needed - SAFE boolean r2 = (count != 0) && (10 / count > 1); System.out.println("Short-circuit result: " + r2); } }
name != null && β pehle null check karo, tab hi .length() call karo. Yeh real-world mein sabse common short-circuit use case hai!public class ShortCircuit4 { public static void main(String[] args) { // & aur | (non-short-circuit) - DONO sides hamesha evaluate int a = 5; System.out.println("--- Single & (no short-circuit) ---"); boolean r1 = (a > 10) & (a++ > 3); // a++ execute hoga! System.out.println("a after & : " + a); // 6 (a++ hua) a = 5; // reset System.out.println("--- Double && (short-circuit) ---"); boolean r2 = (a > 10) && (a++ > 3); // a++ execute NAHI hoga! System.out.println("a after &&: " + a); // 5 (a++ nahi hua!) System.out.println("\nConclusion:"); System.out.println("& runs both sides always"); System.out.println("&& skips right side if left is false"); } }
Deep Explanation:
++ β 1 badha do -- β 1 ghata do
2 Types:
Pre-increment : ++a β PEHLE badha, PHIR use karo Post-increment : a++ β PEHLE use karo, PHIR badha
Memory trick:
++a β ++ pehle hai β pehle kaam karo (increment) phir value do a++ β ++ baad mein β pehle value do phir kaam karo (increment)
Step by step:
int a = 5; int x = ++a; // a pehle 6 hua, phir x = 6 β a=6, x=6 int a = 5; int x = a++; // x = 5 mila pehle, phir a = 6 hua β a=6, x=5
public class Increment1 { public static void main(String[] args) { // Pre-increment: pehle badha, phir use int a = 5; int x = ++a; System.out.println("Pre-increment:"); System.out.println("a = " + a); // 6 System.out.println("x = " + x); // 6 System.out.println(); // Post-increment: pehle use, phir badha int b = 5; int y = b++; System.out.println("Post-increment:"); System.out.println("b = " + b); // 6 System.out.println("y = " + y); // 5 (purani value mili) } }
public class Increment2 { public static void main(String[] args) { // Step by step trace karo int a = 10; System.out.println("a = " + a); // 10 System.out.println("a++ = " + a++); // 10 (purani value print, phir 11) System.out.println("a = " + a); // 11 (ab 11 hai) System.out.println("++a = " + ++a); // 12 (pehle 12, phir print) System.out.println("a = " + a); // 12 System.out.println(); // Pre-decrement vs Post-decrement int b = 10; System.out.println("b = " + b); // 10 System.out.println("b-- = " + b--); // 10 (purani value, phir 9) System.out.println("b = " + b); // 9 System.out.println("--b = " + --b); // 8 (pehle 8, phir print) System.out.println("b = " + b); // 8 } }
public class Increment3 { public static void main(String[] args) { // Complex expressions - carefully trace karo int a = 5; int result = a++ + ++a; // Step 1: a++ β 5 use karo, a becomes 6 // Step 2: ++a β a becomes 7, 7 use karo // result = 5 + 7 = 12, a = 7 System.out.println("result = " + result); // 12 System.out.println("a = " + a); // 7 System.out.println(); int b = 3; int r2 = ++b * b++; // Step 1: ++b β b becomes 4, use 4 // Step 2: b++ β use 4 (current b), then b becomes 5 // r2 = 4 * 4 = 16, b = 5 System.out.println("r2 = " + r2); // 16 System.out.println("b = " + b); // 5 } }
public class Increment4 { public static void main(String[] args) { // Loop mein increment - most common use System.out.println("Post-increment loop (i=0 to 4):"); for (int i = 0; i < 5; i++) { // i++ most common System.out.print(i + " "); } System.out.println("\nPre-increment loop (same result here):"); for (int i = 0; i < 5; ++i) { // ++i same result in loop System.out.print(i + " "); } System.out.println("\n\nPre vs Post in loop condition:"); int x = 0; while (x++ < 3) { // post: check x first, then increment System.out.print(x + " "); } System.out.println(); int y = 0; while (++y < 3) { // pre: increment first, then check System.out.print(y + " "); } } }
i++ aur ++i same result deta hai mostly. But condition mein difference aata hai β post mein ek extra iteration ho sakti hai.Deep Explanation:
condition ? value_if_true : value_if_false
if-else ka short form hai:
// If-else way: if (a > b) { max = a; } else { max = b; } // Ternary way: max = (a > b) ? a : b;
public class Ternary1 { public static void main(String[] args) { int a = 15, b = 10; // Max find karo int max = (a > b) ? a : b; System.out.println("Max = " + max); // 15 // Min find karo int min = (a < b) ? a : b; System.out.println("Min = " + min); // 10 // Even or Odd int num = 28; String type = (num % 2 == 0) ? "Even" : "Odd"; System.out.println(num + " is " + type); // Pass or Fail int marks = 72; String result = (marks >= 35) ? "PASS" : "FAIL"; System.out.println("Result: " + result); } }
public class Ternary2 { public static void main(String[] args) { // Nested ternary (3 conditions) int marks = 85; String grade = (marks >= 90) ? "A+" : (marks >= 75) ? "A" : (marks >= 60) ? "B" : "C"; System.out.println("Marks: " + marks + " | Grade: " + grade); // Absolute value int num = -15; int abs = (num >= 0) ? num : -num; System.out.println("Absolute of " + num + " = " + abs); // Discount logic int price = 1200; double discount = (price > 1000) ? 0.10 : 0.05; double finalPrice = price - (price * discount); System.out.println("Price: " + price + " | Final: " + finalPrice); } }
Deep Explanation: Bitwise operators binary (0 and 1) level pe kaam karte hain:
& β Bitwise AND β dono 1 hone chahiye | β Bitwise OR β ek 1 ho toh 1 ^ β Bitwise XOR β dono alag ho toh 1 ~ β Complement β 0β1, 1β0 << β Left shift β 2 se multiply >> β Right shift β 2 se divide
Example:
5 = 0101 3 = 0011 --------- 5 & 3 = 0001 = 1 5 | 3 = 0111 = 7 5 ^ 3 = 0110 = 6
public class Bitwise1 { public static void main(String[] args) { int a = 5; // binary: 0101 int b = 3; // binary: 0011 System.out.println("a = " + a + " (binary: 0101)"); System.out.println("b = " + b + " (binary: 0011)"); System.out.println(); System.out.println("a & b = " + (a & b)); // 0001 = 1 System.out.println("a | b = " + (a | b)); // 0111 = 7 System.out.println("a ^ b = " + (a ^ b)); // 0110 = 6 System.out.println("~a = " + (~a)); // -6 System.out.println("a << 1 = " + (a << 1)); // 1010 = 10 (Γ2) System.out.println("a >> 1 = " + (a >> 1)); // 0010 = 2 (Γ·2) } }
public class Bitwise2 { public static void main(String[] args) { // Shift = multiply/divide by 2 (fast) int num = 8; System.out.println("num = " + num); System.out.println("num << 1 = " + (num << 1)); // Γ2 = 16 System.out.println("num << 2 = " + (num << 2)); // Γ4 = 32 System.out.println("num << 3 = " + (num << 3)); // Γ8 = 64 System.out.println("num >> 1 = " + (num >> 1)); // Γ·2 = 4 System.out.println("num >> 2 = " + (num >> 2)); // Γ·4 = 2 // Even/Odd using bitwise (faster than %) int x = 13; if ((x & 1) == 1) System.out.println(x + " is Odd (bitwise check)"); else System.out.println(x + " is Even (bitwise check)"); int y = 20; if ((y & 1) == 0) System.out.println(y + " is Even (bitwise check)"); } }
| Operator Type | Symbols | Key Point |
|---|---|---|
| Arithmetic | + - * / % | int/int = int (decimal cut!) |
| Assignment | = += -= *= /= %= | a+=5 means a=a+5 |
| Comparison | == != > < >= <= | Result always boolean; = assign, == compare |
| Logical | && || ! | Result always boolean |
| β Short-Circuit | && || | && β left false? skip right | || β left true? skip right | Saves from crash (null, divide by 0) |
| β Increment/Dec | ++ -- | ++a β pehle badha phir use (pre) | a++ β pehle use phir badha (post) |
| Ternary | ? : | condition ? true_val : false_val β if-else ka short form |
| Bitwise | & | ^ ~ << >> | Binary level operations | << = Γ2, >> = Γ·2 |
| Mistake | Wrong β | Right β |
|---|---|---|
| Assignment vs Comparison | if(a = 5) | if(a == 5) |
| String + numbers | "ans=" + a + b | "ans=" + (a+b) |
| Int division | double d = 5/2 β 2.0 | (double)5/2 β 2.5 |
| Short-circuit ignore | null.length() > 0 | obj != null && obj.length() > 0 |
| Pre vs Post confusion | x = a++ (nai chahiye) | x = ++a (nai value ke liye) |
ShortCircuit1 hai toh file ShortCircuit1.java hogi!