Operators
Arithmetic operators (+ - * / %) numbers पर maths करते हैं। % operator "remainder" देता है — जैसे 10 % 3 = 1।
Relational operators (== != > < >= <=) दो values compare करके true/false बताते हैं। Logical operators (&& || !) multiple conditions जोड़ते हैं।
int a = 10, b = 3;
System.out.println(a % b); // 1
System.out.println(a > b && b > 0); // true- + - * / % maths के लिए
- % remainder देता है
- == > < compare, && || logic जोड़ते हैं
+ - * / % basic maths के लिए। Division (/) का behavior operand types पर depend करता है — दो integers का division "integer division" होता है (decimal cut जाता है, जैसे 7/2 = 3, ना कि 3.5), लेकिन अगर एक भी operand double हो, result decimal आता है (7.0/2 = 3.5)। % (modulo) remainder देता है, जो negative numbers के साथ कभी confusing हो सकता है (-7 % 2 = -1 Java में, ना कि 1)।
Unary ++ और -- दो तरीक़ों से use होते हैं — pre-increment (++x, पहले बढ़ाओ फिर use करो) और post-increment (x++, पहले use करो फिर बढ़ाओ)। Same expression में दोनों use करना (जैसे x = x++ + ++x) confusing behavior दे सकता है — real code में ऐसा कभी मत लिखो।
int x = 5;
System.out.println(x++); // prints 5, फिर x = 6
System.out.println(++x); // x = 7 pehle, फिर prints 7
System.out.println(7 / 2); // 3 (integer division)
System.out.println(7.0 / 2); // 3.5Relational operators (== != > < >= <=) दो values compare करते हैं और boolean result देते हैं। Logical operators में && और || "short-circuit" होते हैं — मतलब अगर पहला operand ही result decide कर दे, तो दूसरा evaluate ही नहीं होता (performance और safety दोनों के लिए ज़रूरी, जैसे null-check)।
& और | भी logical operators की तरह काम करते हैं लेकिन short-circuit नहीं करते — दोनों sides हमेशा evaluate होते हैं, चाहे ज़रूरत हो या ना हो। इसलिए अगर दूसरे operand में कोई side-effect (जैसे method call) है जो सिर्फ़ conditionally चलना चाहिए, हमेशा && / || use करो, & / | नहीं।
! (NOT) operator boolean value को उल्टा कर देता है — !true = false।
String s = null;
if (s != null && s.length() > 0) { // short-circuit: s.length() call hi nahi hoga agar s null hai
System.out.println("Safe!");
}ये operators bits (0/1) के level पर काम करते हैं — & (AND, दोनों 1 हों तभी 1), | (OR, कोई एक भी 1 हो तो 1), ^ (XOR, exactly एक 1 हो तो 1), ~ (NOT, सब bits उलट दो)। << left shift करता है (हर shift left = number को 2 से multiply करने जैसा असर), >> signed right shift (divide by 2 जैसा, sign bit preserve होता है negative numbers के लिए), और >>> unsigned right shift (negative numbers के sign bit को भी 0 से भर देता है)।
इन operators का use low-level programming, flags/permissions को एक ही int में pack करके store करना (जैसे file permissions read/write/execute), या performance-critical bit-manipulation calculations में होता है। रोज़-मर्रा के application code में कम दिखते हैं, लेकिन interviews में बहुत common हैं।
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b); // 1 (0001)
System.out.println(a | b); // 7 (0111)
System.out.println(a ^ b); // 6 (0110)
System.out.println(a << 1); // 10 (1010)
System.out.println(-8 >> 1); // -4 (sign preserved)
System.out.println(-8 >>> 1); // bahut bada positive numberCompound assignment operators (+=, -=, *=, /=, %=) shortcuts हैं — x += 5 मतलब x = x + 5। ये सिर्फ़ छोटा लिखने का तरीक़ा नहीं है — इनमें एक implicit cast भी होती है जो कभी useful हो सकती है: byte b = 10; b += 5; काम करेगा, लेकिन b = b + 5; compile error देगा (क्योंकि b+5 एक int expression बनता है, जिसे वापस byte में manually cast करना पड़ेगा)।
Ternary operator (condition ? valueIfTrue : valueIfFalse) एक छोटा if-else है जो एक expression में fit हो जाता है — जब तुम्हे सिर्फ़ एक value assign/return करनी हो, condition के basis पर।
int marks = 75;
String result = (marks >= 40) ? "Pass" : "Fail";
byte b = 10;
b += 5; // works — implicit narrowing cast hoti hai
// b = b + 5; // ye error dega bina explicit cast keजब एक expression में multiple operators हों, Java एक fixed order follow करता है कि कौनसा पहले evaluate होगा — इसे "precedence" कहते हैं। सबसे high precedence unary operators (++, --, !) की होती है, फिर arithmetic (* / % पहले, + - बाद में), फिर relational (< > ==), फिर logical (&& फिर ||), और सबसे last assignment (=)।
जब confusion हो, parentheses () use करो — ये सिर्फ़ readable नहीं बनाते, precedence को explicitly override भी करते हैं और bugs से बचाते हैं।
| Precedence (High → Low) | Operators |
|---|---|
| 1. Unary | ++ -- ! ~ |
| 2. Multiplicative | * / % |
| 3. Additive | + - |
| 4. Relational | < > <= >= |
| 5. Equality | == != |
| 6. Logical AND | && |
| 7. Logical OR | || |
| 8. Ternary | ? : |
| 9. Assignment | = += -= *= /= |