Introduction
Java mein coding karte time hume kai baar values ko compare, calculate ya manipulate karna padta hai. Yeh kaam hum operators se karte hain. Java mein different types ke operators hote hain jo har tarah ke task ke liye use hote hain.
Chalo ek ek karke sab samajhte hain:
1. Arithmetic Operators
Yeh operators numbers par basic calculations ke liye use hote hain jaise addition, subtraction, multiplication, etc.
- Addition:
a + b
- Subtraction:
a - b
- Multiplication:
a * b
- Division:
a / b
- Modulus (remainder):
a % b
Example:
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a - b); // 7
System.out.println(a * b); // 30
System.out.println(a / b); // 3
System.out.println(a % b); // 1
2. Relational Operators
Yeh check karte hain ki do values ke beech relation kya hai – equal hai, greater hai, smaller hai, etc. Result hamesha true ya false hota hai.
- Equal to:
a == b
- Not equal to:
a != b
- Greater than:
a > b
- Less than:
a < b
- Greater than or equal:
a >= b
- Less than or equal:
a <= b
Example:
int x = 5, y = 8;
System.out.println(x == y); // false
System.out.println(x != y); // true
System.out.println(x > y); // false
System.out.println(x < y); // true
System.out.println(x >= y); // false
System.out.println(x <= y); // true
3. Logical Operators
Yeh operators boolean values ke saath kaam karte hain. Mostly conditions mein use hote hain (if, while, etc.).
- Logical AND:
cond1 && cond2
- Logical OR:
cond1 || cond2
- Logical NOT:
!cond
Example:
int age = 20;
boolean isAdult = age >= 18 && age <= 60;
System.out.println(isAdult); // true
String weather = "Hot";
boolean hungry = false;
boolean eatIceCream = (weather == "Hot" || hungry == false);
System.out.println(eatIceCream); // true
String studentPass = true;
System.out.println(!studentPass); // false
4. Bitwise Operators
Yeh thode advanced level ke operators hain jo bit level par kaam karte hain. Integers ke binary representation ke upar operations perform karte hain.
AND: &
Bitwise AND
OR : |
Bitwise OR
XOR: ^
Bitwise XOR
Complement: ~
Bitwise NOT
Left Shift: <<
Bits ko left shift karna
Right Shift: >>
Bits ko right shift karna
Example:
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
System.out.println("a & b = " + (a & b)); // 1 -> 0001
System.out.println("a | b = " + (a | b)); // 7 -> 0111
System.out.println("a ^ b = " + (a ^ b)); // 6 -> 0110 (XOR)
System.out.println("~a = " + (~a)); // -6 -> Inverts all bits
System.out.println("a << 1 = " + (a << 1)); // 10 -> 1010 (Left shift)
System.out.println("a >> 1 = " + (a >> 1)); // 2 -> 0010 (Signed right shift)
System.out.println("a >>> 1 = " + (a >>> 1));// 2 -> Same as >> for positive numbers
5. Ternary Operator
Yeh ek short-hand form hai if-else ka. Iska syntax simple hai:
condition ? value_if_true : value_if_false;
Example:
int age = 18;
String result = (age >= 18) ? "Eligible" : "Not Eligible";
System.out.println(result); // Eligible
Conclusion
Operators Java programming ka ek important part hain. Agar aapko yeh samajh aa gaya, toh aap easily calculations, comparisons aur conditions likh sakte ho. Agar ye sab overwhelming laga to thodi practice karo and jaldi hi sab natural lagega!
Comments