Operators in Java - Arithmetic, Relational, Logical, Bitwise, Assignment and Other Operators (जावा में ऑपरेटर)

In Java, operators are special symbols that perform specific operations on variables, values, and expressions. They are used to perform mathematical calculations, compare values, combine conditions, assign values, and manipulate individual bits. Java provides different categories of operators, such as Arithmetic, Unary, Relational, Equality, Logical, Bitwise, Shift, Assignment, Conditional, and Type Comparison operators.

जावा में ऑपरेटर विशेष प्रतीक होते हैं जो वेरिएबल्स, मानों और एक्सप्रेशन पर विशेष क्रियाएँ करते हैं। इनका उपयोग गणितीय गणनाएँ करने, मानों की तुलना करने, शर्तों को जोड़ने, मान असाइन करने तथा बिट्स पर कार्य करने के लिए किया जाता है। Java में Arithmetic, Unary, Relational, Equality, Logical, Bitwise, Shift, Assignment, Conditional और Type Comparison जैसे विभिन्न प्रकार के ऑपरेटर उपलब्ध हैं।

Types of Operators in Java (जावा में ऑपरेटर के प्रकार)

Java operators can be classified into the following categories:

  1. Arithmetic Operators (गणितीय ऑपरेटर)

  2. Unary Operators (एकल ऑपरेटर)

  3. Relational Operators (तुलनात्मक ऑपरेटर)

  4. Equality Operators (समानता ऑपरेटर)

  5. Logical Operators (तार्किक ऑपरेटर)

  6. Bitwise Operators (बिटवाइज़ ऑपरेटर)

  7. Shift Operators (शिफ्ट ऑपरेटर)

  8. Assignment Operators (असाइनमेंट ऑपरेटर)

  9. Conditional or Ternary Operator (शर्तीय ऑपरेटर)

  10. instanceof Operator (इंस्टेंसऑफ ऑपरेटर)


1. Arithmetic Operators (गणितीय ऑपरेटर)

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and finding the remainder.

गणितीय ऑपरेटरों का उपयोग जोड़, घटाव, गुणा, भाग और शेषफल जैसी गणितीय गणनाएँ करने के लिए किया जाता है।

OperatorNameDescriptionExample
+AdditionAdds two valuesa + b
-SubtractionSubtracts one value from anothera - b
*MultiplicationMultiplies two valuesa * b
/DivisionDivides one value by anothera / b
%ModulusReturns the remaindera % b

Example:

class ArithmeticDemo {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("Addition = " + (a + b));
System.out.println("Subtraction = " + (a - b));
System.out.println("Multiplication = " + (a * b));
System.out.println("Division = " + (a / b));
System.out.println("Modulus = " + (a % b));
}
}

Output:

Addition = 13
Subtraction = 7
Multiplication = 30
Division = 3
Modulus = 1

Note: When both operands are integers, the division operator / performs integer division and discards the fractional part. For example, 10 / 3 produces 3, whereas 10.0 / 3 produces approximately 3.333333.

नोट: जब दोनों ऑपरेंड पूर्णांक होते हैं, तब / ऑपरेटर पूर्णांक भाग (Integer Division) करता है और दशमलव भाग को हटा देता है। उदाहरण के लिए, 10 / 3 का परिणाम 3 होता है, जबकि 10.0 / 3 का परिणाम लगभग 3.333333 होता है।

The + operator can also be used to concatenate strings.

+ ऑपरेटर का उपयोग दो स्ट्रिंग्स को जोड़ने (String Concatenation) के लिए भी किया जाता है।

String first = "Java";
String second = " Programming";
System.out.println(first + second);

Output:

Java Programming

2. Unary Operators (एकल ऑपरेटर)

Unary operators perform operations on only one operand. They are used to change a value, reverse a Boolean condition, or obtain the positive or negative form of a number.

Unary ऑपरेटर केवल एक ऑपरेंड पर कार्य करते हैं। इनका उपयोग किसी मान को बढ़ाने, घटाने, Boolean condition को उलटने या संख्या का धनात्मक अथवा ऋणात्मक रूप प्राप्त करने के लिए किया जाता है।

OperatorNameDescriptionExample
+Unary PlusIndicates a positive value+a
-Unary MinusNegates a numeric value-a
++IncrementIncreases the value by 1a++
--DecrementDecreases the value by 1a--
!Logical ComplementReverses a Boolean value!flag
~Bitwise ComplementInverts all bits of an integer~a

Increment and Decrement Operators (वृद्धि और ह्रास ऑपरेटर)

The increment operator ++ increases a variable's value by one, whereas the decrement operator -- decreases its value by one.

Increment ऑपरेटर ++ किसी वेरिएबल के मान में एक की वृद्धि करता है, जबकि Decrement ऑपरेटर -- उसके मान में एक की कमी करता है।

There are two forms of increment and decrement operators:

1. Prefix Increment (++a)

The value is increased before it is used in the expression.

इसमें एक्सप्रेशन में उपयोग करने से पहले वेरिएबल के मान में वृद्धि होती है।

2. Postfix Increment (a++)

The current value is used first, and then the value is increased.

इसमें पहले वेरिएबल के वर्तमान मान का उपयोग होता है और उसके बाद मान में वृद्धि होती है।

Example:

class UnaryDemo {
public static void main(String[] args) {
int a = 10;
System.out.println("Postfix = " + a++);
System.out.println("After Postfix = " + a);
System.out.println("Prefix = " + ++a);
System.out.println("After Prefix = " + a);
System.out.println("Negative = " + (-a));
boolean flag = true;
System.out.println("Logical NOT = " + !flag);
}
}

Output:

Postfix = 10
After Postfix = 11
Prefix = 12
After Prefix = 12
Negative = -12
Logical NOT = false

3. Relational Operators (तुलनात्मक ऑपरेटर)

Relational operators are used to compare two values. They return a Boolean result, either true or false.

Relational ऑपरेटरों का उपयोग दो मानों की तुलना करने के लिए किया जाता है। ये तुलना के परिणाम के रूप में true या false लौटाते हैं।

OperatorNameDescriptionExample
>Greater ThanChecks whether the left value is greatera > b
<Less ThanChecks whether the left value is smallera < b
>=Greater Than or Equal ToChecks whether the left value is greater than or equal to the righta >= b
<=Less Than or Equal ToChecks whether the left value is less than or equal to the righta <= b

Example:

class RelationalDemo {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
}
}

Output:

false
true
false
true

4. Equality Operators (समानता ऑपरेटर)

Equality operators are used to check whether two values are equal or different. They return a Boolean result.

Equality ऑपरेटरों का उपयोग यह जाँचने के लिए किया जाता है कि दो मान समान हैं या अलग-अलग। ये true या false परिणाम देते हैं।

OperatorNameDescriptionExample
==Equal ToChecks whether two values are equala == b
!=Not Equal ToChecks whether two values are differenta != b

Example:

class EqualityDemo {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println(a == b);
System.out.println(a != b);
}
}

Output:

false
true

Note: For primitive types, == compares values. For reference types, == checks whether two references refer to the same object, not whether two different objects have equal contents. To compare the contents of strings, use the equals() method.

नोट: Primitive data types के लिए == मानों की तुलना करता है। Reference types के लिए == यह जाँचता है कि दोनों references एक ही object को refer करते हैं या नहीं। दो अलग-अलग String objects के टेक्स्ट की तुलना करने के लिए equals() मेथड का उपयोग किया जाता है।


5. Logical Operators (तार्किक ऑपरेटर)

Logical operators are used to combine or reverse Boolean conditions. They are commonly used in decision-making statements such as if, if-else, and loops.

Logical ऑपरेटरों का उपयोग Boolean conditions को जोड़ने या उलटने के लिए किया जाता है। इनका उपयोग सामान्यतः if, if-else और loops जैसे decision-making statements में किया जाता है।

OperatorNameDescriptionExample
&&Conditional ANDReturns true if both conditions are truea > 5 && b < 10
||Conditional ORReturns true if at least one condition is truea > 5 || b < 10

!Logical NOTReverses the Boolean result!(a > b)

Truth Table of Logical Operators (लॉजिकल ऑपरेटर की सत्यता सारणी)

ABA && BA || B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

Logical NOT Truth Table:

A!A
truefalse
falsetrue

Example:

class LogicalDemo {
public static void main(String[] args) {
int age = 22;
boolean hasID = true;
System.out.println(age >= 18 && hasID);
System.out.println(age < 18 || hasID);
System.out.println(!hasID);
}
}

Output:

true
true
false

Short-Circuit Evaluation:

The && and || operators use short-circuit evaluation. In A && B, if A is false, B is not evaluated. In A || B, if A is true, B is not evaluated.

&& और || ऑपरेटर Short-Circuit Evaluation का उपयोग करते हैं। A && B में यदि A का परिणाम false है, तो B का मूल्यांकन नहीं किया जाता। इसी प्रकार, A || B में यदि A का परिणाम true है, तो B का मूल्यांकन नहीं किया जाता।


6. Bitwise Operators (बिटवाइज़ ऑपरेटर)

Bitwise operators perform operations directly on the individual bits of integer data types such as byte, short, int, and long.

Bitwise ऑपरेटर integer data types जैसे byte, short, int और long के अलग-अलग bits पर सीधे कार्य करते हैं।

OperatorNameDescriptionExample
&Bitwise ANDSets a bit to 1 if both corresponding bits are 1a & b
|Bitwise ORSets a bit to 1 if at least one corresponding bit is 1a | b
^Bitwise XORSets a bit to 1 if corresponding bits are differenta ^ b
~Bitwise ComplementInverts every bit~a

Bitwise AND, OR and XOR Truth Table

ABA & BA | BA ^ B
00000
01011
10011
11110

Example:

class BitwiseDemo {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println("AND = " + (a & b));
System.out.println("OR = " + (a | b));
System.out.println("XOR = " + (a ^ b));
System.out.println("Complement = " + (~a));
}
}

Binary Representation:

a = 5 = 0101
b = 3 = 0011
a & b = 0001 = 1
a | b = 0111 = 7
a ^ b = 0110 = 6
~a = -6

Output:

AND = 1
OR = 7
XOR = 6
Complement = -6

Note: In Java, the &, |, and ^ operators can also operate on Boolean operands. Unlike && and ||, Boolean & and | always evaluate both operands.

नोट: Java में &, | और ^ ऑपरेटर Boolean operands पर भी कार्य कर सकते हैं। && और || के विपरीत, Boolean & और | दोनों operands का मूल्यांकन करते हैं।


7. Shift Operators (शिफ्ट ऑपरेटर)

Shift operators are used to shift the bits of an integer value to the left or right. Java provides three shift operators.

Shift ऑपरेटरों का उपयोग किसी integer मान के bits को बाईं या दाईं ओर स्थानांतरित करने के लिए किया जाता है। Java में तीन प्रकार के Shift Operators उपलब्ध हैं।

OperatorNameDescriptionExample
<<Left ShiftShifts bits to the lefta << 2
>>Signed Right ShiftShifts bits to the right while preserving the signa >> 2
>>>Unsigned Right ShiftShifts bits to the right and fills the leftmost bits with zerosa >>> 2

1. Left Shift Operator (<<)

The left shift operator moves the bits to the left by the specified number of positions. For positive integers, it generally multiplies the value by powers of 2 when no significant bits are lost.

Left Shift ऑपरेटर bits को निर्धारित स्थानों तक बाईं ओर स्थानांतरित करता है। Positive integers के लिए, जब महत्वपूर्ण bits नष्ट नहीं होते, तो यह सामान्यतः मान को 2 की घात से गुणा करने के समान होता है।

2. Signed Right Shift Operator (>>)

The signed right shift operator moves bits to the right and preserves the sign by copying the sign bit into the leftmost positions.

Signed Right Shift ऑपरेटर bits को दाईं ओर स्थानांतरित करता है तथा sign bit को बाईं ओर भरकर संख्या का चिह्न बनाए रखता है।

3. Unsigned Right Shift Operator (>>>)

The unsigned right shift operator moves bits to the right and fills the leftmost positions with zeros, irrespective of the sign of the number.

Unsigned Right Shift ऑपरेटर bits को दाईं ओर स्थानांतरित करता है और संख्या के sign की परवाह किए बिना बाईं ओर के स्थानों में शून्य भरता है।

Example:

class ShiftDemo {
public static void main(String[] args) {
int a = 8;
System.out.println("Left Shift = " + (a << 1));
System.out.println("Right Shift = " + (a >> 1));
System.out.println("Unsigned Right Shift = " + (a >>> 1));
}
}

Output:

Left Shift = 16
Right Shift = 4
Unsigned Right Shift = 4

Note: The difference between >> and >>> is especially visible with negative numbers. The >> operator preserves the sign, whereas >>> fills the leftmost bits with zeros.

नोट: >> और >>> के बीच का अंतर विशेष रूप से Negative numbers के साथ दिखाई देता है। >> ऑपरेटर संख्या का sign बनाए रखता है, जबकि >>> बाईं ओर के स्थानों में शून्य भरता है।


8. Assignment Operators (असाइनमेंट ऑपरेटर)

Assignment operators are used to assign values to variables. Java also provides compound assignment operators that combine an operation with assignment.

Assignment ऑपरेटरों का उपयोग वेरिएबल्स को मान प्रदान करने के लिए किया जाता है। Java में Compound Assignment Operators भी उपलब्ध हैं, जो किसी ऑपरेशन और असाइनमेंट को एक साथ करते हैं।

OperatorNameExampleEquivalent Expression
=Simple Assignmenta = 10a = 10
+=Addition Assignmenta += 5a = a + 5
-=Subtraction Assignmenta -= 5a = a - 5
*=Multiplication Assignmenta *= 5a = a * 5
/=Division Assignmenta /= 5a = a / 5
%=Remainder Assignmenta %= 5a = a % 5
&=Bitwise AND Assignmenta &= 5a = a & 5
|=Bitwise OR Assignmenta |= 5a = a | 5
^=Bitwise XOR Assignmenta ^= 5a = a ^ 5
<<=Left Shift Assignmenta <<= 2a = a << 2
>>=Signed Right Shift Assignmenta >>= 2a = a >> 2
>>>=Unsigned Right Shift Assignmenta >>>= 2a = a >>> 2

Example:

class AssignmentDemo {
public static void main(String[] args) {
int a = 10;
a += 5;
System.out.println("After += " + a);
a -= 3;
System.out.println("After -= " + a);
a *= 2;
System.out.println("After *= " + a);
a /= 4;
System.out.println("After /= " + a);
a %= 3;
System.out.println("After %= " + a);
}
}

Output:

After += 15
After -= 12
After *= 24
After /= 6
After %= 0

9. Conditional or Ternary Operator (शर्तीय या टर्नरी ऑपरेटर)

The conditional operator ?: is the only operator in Java that takes three operands. It is used as a short form of an if-else expression.

Conditional ऑपरेटर ?: Java का एकमात्र ऐसा ऑपरेटर है जो तीन operands पर कार्य करता है। इसका उपयोग if-else expression के संक्षिप्त रूप में किया जाता है।

Syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

यदि condition का परिणाम true होता है, तो expression1 का मूल्यांकन किया जाता है; अन्यथा expression2 का मूल्यांकन किया जाता है।

Example:

class TernaryDemo {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Maximum = " + max);
}
}

Output:

Maximum = 20

10. instanceof Operator (इंस्टेंसऑफ ऑपरेटर)

The instanceof operator is used to check whether an object is an instance of a particular class, a subclass, or an implementation of a specified interface. It returns a Boolean result.

instanceof ऑपरेटर का उपयोग यह जाँचने के लिए किया जाता है कि कोई object किसी विशेष class, उसकी subclass या किसी specified interface का instance है या नहीं। यह Boolean परिणाम देता है।

Syntax:

objectReference instanceof ClassName

Example:

class Animal {
}
class Dog extends Animal {
}
class InstanceofDemo {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println(d instanceof Dog);
System.out.println(d instanceof Animal);
System.out.println(d instanceof Object);
}
}

Output:

true
true
true

Java also supports pattern matching with instanceof in modern versions. It allows checking the type and declaring a variable in one expression.

Java के आधुनिक versions में instanceof के साथ Pattern Matching की सुविधा भी उपलब्ध है। इससे type की जाँच करने और उसी expression में एक variable घोषित करने की सुविधा मिलती है।

Example (Java 16 and later):

Object obj = "Java Programming";
if (obj instanceof String str) {
System.out.println(str.length());
}

Output:

16

Type Casting in Java (जावा में टाइप कास्टिंग)

Type casting is used to convert a value from one data type to another. Java supports widening and narrowing conversions.

Type Casting का उपयोग किसी मान को एक data type से दूसरे data type में बदलने के लिए किया जाता है। Java में Widening और Narrowing conversions उपलब्ध हैं।

Example:

class CastingDemo {
public static void main(String[] args) {
double a = 10.75;
int b = (int) a;
System.out.println(b);
}
}

Output:

10

Here, (int) is a cast expression that explicitly converts the double value into an int. Type casting is a conversion operation, not a separate operator category in Java's formal operator classification.

यहाँ (int) एक Cast Expression है, जो double मान को स्पष्ट रूप से int में परिवर्तित करता है। Type Casting एक conversion operation है, Java के औपचारिक operator classification में यह अलग operator category नहीं है।


Difference Between Logical and Bitwise Operators (लॉजिकल और बिटवाइज़ ऑपरेटर में अंतर)

Logical OperatorsBitwise Operators
Used mainly with Boolean conditions.Used with integer bits and also Boolean operands for &, `, and ^`.
&& and `` perform short-circuit evaluation.& and `` do not perform short-circuit evaluation.
Commonly used in decision-making statements.Commonly used in bit manipulation and low-level operations.
Example: a > 5 && b < 10Example: a & b

Special Symbols and Expressions in Java (जावा में विशेष चिह्न और एक्सप्रेशन)

Besides operators, Java uses several special symbols that help write programs. These symbols are important, but they are not all classified as operators.

Operators के अतिरिक्त Java में कई विशेष चिह्नों का उपयोग प्रोग्राम लिखने के लिए किया जाता है। ये चिह्न महत्वपूर्ण हैं, लेकिन इन सभी को operators की श्रेणी में नहीं रखा जाता है।

SymbolNamePurposeExample
()ParenthesesGroup expressions and pass method arguments(a + b), println()
[]Square BracketsDeclare and access arraysint[] a, a[0]
{}Curly BracesDefine blocks and class bodies{ System.out.println("Java"); }
.Dot Operator / Member AccessAccess members of classes and objectsSystem.out
newObject Creation KeywordCreates objects and arraysnew Student()
::Method Reference OperatorRefers to a method or constructorSystem.out::println
->Lambda ArrowIntroduces a lambda expressionx -> x * x

Note: The dot (.) is commonly called the member access operator in programming education. However, symbols such as (), [], {}, and the keyword new have their own language roles and should not all be treated as arithmetic or logical operators. Similarly, -> and :: are used in lambda expressions and method references.

नोट: Dot (.) को सामान्यतः Member Access Operator कहा जाता है। लेकिन (), [], {} जैसे चिह्न तथा new keyword की अपनी अलग भूमिकाएँ हैं। इन्हें सभी को Arithmetic या Logical Operators नहीं माना जाता। इसी प्रकार -> और :: का उपयोग Lambda Expressions तथा Method References में किया जाता है।

Operators are an essential part of Java programming. They allow programmers to perform mathematical calculations, compare values, make decisions, manipulate bits, assign values, and work with objects. Understanding operator types, their usage, and precedence helps programmers write clear, efficient, and error-free Java programs.

Java प्रोग्रामिंग में Operators एक महत्वपूर्ण भूमिका निभाते हैं। इनकी सहायता से गणितीय गणनाएँ, मानों की तुलना, निर्णय लेना, bits को manipulate करना, मान असाइन करना तथा objects के साथ कार्य करना संभव होता है। ऑपरेटरों के प्रकार, उनके उपयोग और precedence को समझना स्पष्ट, प्रभावी और त्रुटिरहित Java प्रोग्राम लिखने में सहायता करता है।

Comments

Popular posts from this blog

Java program to implement file input stream class to read binary data from image file.

Java program to find length of given string and convert it in to upper or lower case.

Java program to check marks out of Bound exception.

Java program to calculate area and perimeter of circle using interfaces.

Java program to merge two text files in to third file.

Java program to read a text file and displays it's contents on screen.

Concatenating and Buffering Files in Java जावा में फ़ाइलों को जोड़ना और बफर करना

Java program to check greatest among three numbers where input numbers are given by user.

Write a Java program to calculate the gross salary of an employee, if basic salary of that employee is greater or equal to 20000 and year's of service is equal or greater than 5 years bonus will be 2000 else1000, TA =10%, DA= 20% and HRA=5% of salary.