What are Operators and Expressions?
Operators Java me special symbols hote hain jo variables aur values par operations perform karte hain. Expressions operators aur operands ka combination hote hain jo ek value return karta hai.
Java me expressions bohot important hain kyunki program ke almost sabhi calculations aur logic expressions ke through hi perform hote hain.
Types of Operators in Java
Java me kayi prakar ke operators available hote hain, har ek apni specific functionality ke liye use hota hai:
- Arithmetic Operators: Mathematical calculations ke liye jaise addition, subtraction, multiplication.
- Unary Operators: Ek operand par kaam karne wale operators, jaise increment aur decrement.
- Assignment Operators: Values ko variables me assign karne ke liye.
- Relational Operators: Comparison operations ke liye, jisse true ya false result milta hai.
- Logical Operators: Multiple boolean expressions ko combine karne ke liye.
- Bitwise Operators: Binary level par operations perform karne ke liye.
- Ternary Operator: Conditional operator jisme ek chhota if-else expression hota hai.
- Shift Operators: Bits ko left ya right shift karne ke liye use hota hai.
1. Arithmetic Operators
Ye basic arithmetic operations ke liye use hote hain:
+
(Addition): Do numbers ko jodta hai.-
(Subtraction): Ek number me se dusra ghataata hai.*
(Multiplication): Do numbers ka gunank karta hai./
(Division): Ek number ko dusre se divide karta hai.%
(Modulus): Division ka remainder deta hai.
int a = 20, b = 6;
System.out.println(a + b); // 26
System.out.println(a - b); // 14
System.out.println(a * b); // 120
System.out.println(a / b); // 3
System.out.println(a % b); // 2
2. Unary Operators
Ye operators sirf ek operand par kaam karte hain. Kuch common unary operators:
++
: Increment operator, value ko 1 se badhata hai.--
: Decrement operator, value ko 1 se kam karta hai.!
: Logical NOT operator, boolean value ko invert karta hai.+
: Unary plus, positive value ko signify karta hai.-
: Unary minus, negative value ko represent karta hai.
int x = 5;
System.out.println(++x); // 6
System.out.println(x--); // 6, phir x banega 5
System.out.println(!true); // false
3. Assignment Operators
Ye operators variable me value assign karne ke liye use hote hain aur kuch shorthand forms dete hain:
=
: Simple assignment.+=
: Add and assign.-=
: Subtract and assign.*=
: Multiply and assign./=
: Divide and assign.%=
: Modulus and assign.
int a = 10;
a += 5; // a = 15
a -= 3; // a = 12
4. Relational Operators
Ye operators do values ka comparison karte hain aur true/false return karte hain:
==
: Equal to.!=
: Not equal to.>
: Greater than.<
: Less than.>=
: Greater than or equal to.<=
: Less than or equal to.
int a = 10, b = 20;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a < b); // true
5. Logical Operators
Ye operators boolean expressions ko combine karne ke liye use hote hain:
&&
: Logical AND, dono expressions true hone par true.||
: Logical OR, koi bhi expression true ho to true.!
: Logical NOT, expression ko invert karta hai.
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
System.out.println(!x); // false
6. Bitwise Operators
Ye operators integer types ke bits par operation karte hain:
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR~
: Bitwise Complement<<
: Left shift>>
: Right shift>>>
: Unsigned right shift
7. Ternary Operator (Conditional)
Ternary operator ek shortcut if-else statement hai, jiska syntax hai: condition ? expr1 : expr2
.
int a = 10;
String result = (a > 5) ? "Greater than 5" : "Less or equal to 5";
System.out.println(result); // "Greater than 5"
Expressions in Java
Expressions variables, constants, operators ka combination hote hain jo ek value return karte hain. Examples:
int a = 5, b = 3;
int sum = a + b; // 8
boolean check = (a > b) && (b < 10); // true
Java me expressions program ke logic aur calculation ka base hain. Complex expressions ko parentheses se group kiya jata hai jisse unka evaluation order control hota hai.