Operators in Java.

By Shakib Ansari | Date: Wed, Jun 4, 2025

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.

  1. Addition: a + b
  2. Subtraction: a - b
  3. Multiplication: a * b
  4. Division: a / b
  5. 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.

  1. Equal to: a == b
  2. Not equal to: a != b
  3. Greater than: a > b
  4. Less than: a < b
  5. Greater than or equal: a >= b
  6. 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.).

  1. Logical AND: cond1 && cond2
  2. Logical OR: cond1 || cond2
  3. 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!

About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments