Operator Precedence in Java (जावा में ऑपरेटर की प्राथमिकता)
In Java, an expression may contain multiple operators. When an expression contains more than one operator, Java follows a specific set of rules to determine the order in which these operators are evaluated. This order is known as Operator Precedence. Operator precedence determines which operator is evaluated first when multiple operators appear in an expression. Operators with higher precedence are evaluated before operators with lower precedence, unless parentheses change the order.
जावा में किसी Expression में एक से अधिक ऑपरेटर हो सकते हैं। जब किसी Expression में कई ऑपरेटर होते हैं, तो जावा कुछ विशेष नियमों का पालन करता है, जो यह निर्धारित करते हैं कि किस ऑपरेटर का मूल्यांकन पहले किया जाएगा। इस क्रम को Operator Precedence (ऑपरेटर की प्राथमिकता) कहते हैं। Operator Precedence यह निर्धारित करता है कि किसी Expression में कौन-सा ऑपरेटर पहले कार्य करेगा। अधिक प्राथमिकता वाले ऑपरेटरों का मूल्यांकन कम प्राथमिकता वाले ऑपरेटरों से पहले किया जाता है, जब तक कि Parentheses द्वारा क्रम को परिवर्तित न किया जाए।
Important महत्वपूर्ण:
Operators with higher precedence are evaluated before operators with lower precedence.
अधिक प्राथमिकता वाले ऑपरेटरों का मूल्यांकन पहले किया जाता है।
Parentheses
()can be used to change the normal order of evaluation.Parentheses
()का उपयोग मूल्यांकन के सामान्य क्रम को बदलने के लिए किया जा सकता है।If operators have the same precedence, associativity determines their grouping.
यदि दो ऑपरेटरों की प्राथमिकता समान है, तो Associativity यह निर्धारित करती है कि उनका grouping किस दिशा में होगा।
Operator Precedence Table in Java (जावा में ऑपरेटर प्राथमिकता सारणी)
The following table shows Java operators arranged from highest precedence to lowest precedence.
निम्नलिखित सारणी में Java के ऑपरेटरों को सबसे अधिक प्राथमिकता से सबसे कम प्राथमिकता के क्रम में दर्शाया गया है।
| Level | Operators | Category | Associativity |
|---|---|---|---|
| 1 | expr++, expr-- | Postfix | Left to Right |
| 2 | ++expr, --expr, +, -, !, ~, (type), new | Unary, Cast and Object Creation | Right to Left |
| 3 | *, /, % | Multiplicative | Left to Right |
| 4 | +, - | Additive | Left to Right |
| 5 | <<, >>, >>> | Shift | Left to Right |
| 6 | <, <=, >, >=, instanceof | Relational | Left to Right |
| 7 | ==, != | Equality | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Conditional AND | Left to Right |
| 12 | || | Conditional OR | Left to Right |
| 13 | ?: | Conditional (Ternary) | Right to Left |
| 14 | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>= | Assignment | Right to Left |
Note: Parentheses used for grouping expressions have the highest practical effect because they explicitly determine which subexpression is evaluated first. Method invocation, array access, and member access also have high precedence in Java expressions.
नोट: Expression को group करने के लिए उपयोग किए जाने वाले Parentheses मूल्यांकन के क्रम को स्पष्ट रूप से निर्धारित करते हैं। Java Expressions में Method Invocation, Array Access और Member Access की भी उच्च प्राथमिकता होती है।
1. Postfix Operators (पोस्टफिक्स ऑपरेटर)
Postfix operators have the highest precedence among the operators listed in the table. The postfix increment (++) and postfix decrement (--) operators are included in this category.
Postfix ऑपरेटरों की प्राथमिकता सारणी में दिए गए ऑपरेटरों में सबसे अधिक होती है। Postfix Increment (++) और Postfix Decrement (--) इसी श्रेणी में आते हैं।
Operators:
expr++– Post-incrementexpr--– Post-decrement
Example:
class PostfixDemo {public static void main(String[] args) {int a = 5;int b = a++;System.out.println("a = " + a);System.out.println("b = " + b);}}
Output:
a = 6b = 5
In this example, the original value of a is assigned to b first. Then, a is incremented by one.
इस उदाहरण में सबसे पहले a का वर्तमान मान b को दिया जाता है। इसके बाद a के मान में एक की वृद्धि होती है।
2. Unary Operators (यूनरी ऑपरेटर)
Unary operators operate on a single operand. This category includes prefix increment, prefix decrement, unary plus, unary minus, logical NOT, bitwise complement, type casting, and object creation using new.
Unary ऑपरेटर केवल एक Operand पर कार्य करते हैं। इस श्रेणी में Prefix Increment, Prefix Decrement, Unary Plus, Unary Minus, Logical NOT, Bitwise Complement, Type Casting तथा new द्वारा Object Creation शामिल हैं।
Operators:
++expr– Prefix Increment--expr– Prefix Decrement+expr– Unary Plus-expr– Unary Minus!expr– Logical NOT~expr– Bitwise Complement(type) expr– Type Castingnew– Object Creation
Example:
class UnaryDemo {public static void main(String[] args) {int a = 10;int b = -a;int c = +a;System.out.println("Negative = " + b);System.out.println("Positive = " + c);}}
Output:
Negative = -10Positive = 10
Unary operators have higher precedence than multiplicative and additive operators.
Unary ऑपरेटरों की प्राथमिकता Multiplicative और Additive ऑपरेटरों से अधिक होती है।
3. Multiplicative Operators (गुणन संबंधी ऑपरेटर)
Multiplicative operators are used for multiplication, division, and finding the remainder. They have higher precedence than additive operators.
Multiplicative ऑपरेटरों का उपयोग गुणा, भाग और शेषफल ज्ञात करने के लिए किया जाता है। इनकी प्राथमिकता Additive ऑपरेटरों से अधिक होती है।
Operators: *, /, %
Example:
int result = 10 + 5 * 2;System.out.println(result);
Output:
20In this expression, multiplication is performed first because * has higher precedence than +.
इस Expression में सबसे पहले गुणा किया जाता है, क्योंकि * ऑपरेटर की प्राथमिकता + ऑपरेटर से अधिक होती है।
4. Additive Operators (जोड़ और घटाव ऑपरेटर)
Additive operators are used to perform addition and subtraction. They have lower precedence than multiplicative operators.
Additive ऑपरेटरों का उपयोग जोड़ और घटाव करने के लिए किया जाता है। इनकी प्राथमिकता Multiplicative ऑपरेटरों से कम होती है।
Operators: +, -
Example:
int result = 20 - 5 + 3;System.out.println(result);
Output:
18The operators + and - have the same precedence. Therefore, they are grouped from left to right.
+ और - ऑपरेटरों की प्राथमिकता समान होती है। इसलिए इनका grouping बाईं से दाईं ओर होता है।
5. Shift Operators (शिफ्ट ऑपरेटर)
Shift operators are used to shift the bits of integer values to the left or right. They have lower precedence than additive operators but higher precedence than relational operators.
Shift ऑपरेटरों का उपयोग Integer मानों के Bits को बाईं या दाईं ओर स्थानांतरित करने के लिए किया जाता है। इनकी प्राथमिकता Additive ऑपरेटरों से कम तथा Relational ऑपरेटरों से अधिक होती है।
Operators:
<<– Left Shift>>– Signed Right Shift>>>– Unsigned Right Shift
Example:
int result = 8 + 4 << 1;System.out.println(result);
Output:
24Here, addition is performed first because additive operators have higher precedence than shift operators.
यहाँ सबसे पहले जोड़ किया जाता है, क्योंकि Additive ऑपरेटरों की प्राथमिकता Shift ऑपरेटरों से अधिक होती है। इसके बाद प्राप्त परिणाम को एक बिट बाईं ओर Shift किया जाता है।
6. Relational Operators (संबंध या तुलनात्मक ऑपरेटर)
Relational operators are used to compare two values. They return either true or false.
Relational ऑपरेटरों का उपयोग दो मानों की तुलना करने के लिए किया जाता है। ये परिणाम के रूप में true या false लौटाते हैं।
Operators:
<– Less Than>– Greater Than<=– Less Than or Equal To>=– Greater Than or Equal Toinstanceof– Type Comparison
Example:
int a = 10;int b = 20;System.out.println(a < b);System.out.println(a > b);
Output:
truefalse
7. Equality Operators (समानता ऑपरेटर)
Equality operators are used to check whether two values are equal or not equal. They have lower precedence than relational operators.
Equality ऑपरेटरों का उपयोग यह जाँचने के लिए किया जाता है कि दो मान समान हैं या असमान। इनकी प्राथमिकता Relational ऑपरेटरों से कम होती है।
Operators:
==– Equal To!=– Not Equal To
Example:
int a = 10;int b = 10;System.out.println(a == b);System.out.println(a != b);
Output:
truefalse
8. Bitwise AND Operator (बिटवाइज़ AND ऑपरेटर)
The Bitwise AND operator & compares corresponding bits of two integer values. It returns 1 when both corresponding bits are 1; otherwise, it returns 0.
Bitwise AND ऑपरेटर & दो Integer मानों के संबंधित Bits की तुलना करता है। यदि दोनों संबंधित Bits 1 होते हैं, तो परिणाम 1 होता है; अन्यथा परिणाम 0 होता है।
Example:
int a = 5;int b = 3;System.out.println(a & b);
Output:
19. Bitwise XOR Operator (बिटवाइज़ XOR ऑपरेटर)
The Bitwise XOR operator ^ compares corresponding bits. It returns 1 when the bits are different and 0 when they are the same.
Bitwise XOR ऑपरेटर ^ संबंधित Bits की तुलना करता है। यदि दोनों Bits अलग-अलग होते हैं, तो परिणाम 1 होता है और यदि दोनों समान होते हैं, तो परिणाम 0 होता है।
Example:
int a = 5;int b = 3;System.out.println(a ^ b);
Output:
610. Bitwise OR Operator (बिटवाइज़ OR ऑपरेटर)
The Bitwise OR operator | compares corresponding bits of two integer values. It returns 1 if at least one of the corresponding bits is 1.
Bitwise OR ऑपरेटर | दो Integer मानों के संबंधित Bits की तुलना करता है। यदि संबंधित Bits में से कम से कम एक Bit 1 होता है, तो परिणाम 1 होता है।
Example:
int a = 5;int b = 3;System.out.println(a | b);
Output:
711. Conditional AND Operator (तार्किक AND ऑपरेटर)
The Conditional AND operator && returns true only when both conditions are true. It uses short-circuit evaluation.
Conditional AND ऑपरेटर && तभी true लौटाता है जब दोनों Conditions true हों। यह Short-Circuit Evaluation का उपयोग करता है।
Example:
int a = 10;int b = 20;System.out.println(a < b && b > 15);
Output:
true12. Conditional OR Operator (तार्किक OR ऑपरेटर)
The Conditional OR operator || returns true if at least one condition is true. It also uses short-circuit evaluation.
Conditional OR ऑपरेटर || तब true लौटाता है जब कम से कम एक Condition true होती है। यह भी Short-Circuit Evaluation का उपयोग करता है।
Example:
int a = 10;int b = 20;System.out.println(a > b || b > 15);
Output:
true13. Conditional or Ternary Operator (शर्तीय या टर्नरी ऑपरेटर)
The conditional operator ?: is also known as the ternary operator because it uses three operands. It is a shorthand form of an if-else expression.
Conditional ऑपरेटर ?: को Ternary Operator भी कहा जाता है, क्योंकि इसमें तीन Operands होते हैं। यह if-else Expression का संक्षिप्त रूप है।
Syntax:
condition ? expression1 : expression2;Example:
int a = 10;int b = 20;int max = (a > b) ? a : b;System.out.println(max);
Output:
20The conditional operator has lower precedence than logical OR and higher precedence than assignment operators.
Conditional ऑपरेटर की प्राथमिकता Logical OR ऑपरेटर से कम तथा Assignment ऑपरेटरों से अधिक होती है।
14. Assignment Operators (असाइनमेंट ऑपरेटर)
Assignment operators are used to assign values to variables. Compound assignment operators combine an operation with assignment.
Assignment ऑपरेटरों का उपयोग Variables को मान प्रदान करने के लिए किया जाता है। Compound Assignment Operators किसी ऑपरेशन और Assignment को एक साथ करते हैं।
Operators:
| Operator | Example |
|---|---|
= | a = 10 |
+= | a += 5 |
-= | a -= 5 |
*= | a *= 5 |
/= | a /= 5 |
%= | a %= 5 |
&= | a &= 5 |
|= | a |= 5 |
^= | a ^= 5 |
<<= | a <<= 2 |
>>= | a >>= 2 |
>>>= | a >>>= 2 |
Example:
int a = 10;a += 5;System.out.println(a);a *= 2;System.out.println(a);
Output:
1530
Assignment operators have lower precedence than the conditional operator and are evaluated from right to left.
Assignment ऑपरेटरों की प्राथमिकता Conditional ऑपरेटर से कम होती है और इनका मूल्यांकन दाईं से बाईं ओर होता है।
Associativity of Operators in Java (जावा में ऑपरेटर की साहचर्यता)
Associativity determines the direction in which operators of the same precedence are grouped in an expression. In Java, operators generally follow either left-to-right or right-to-left associativity.
Associativity यह निर्धारित करती है कि समान प्राथमिकता वाले ऑपरेटरों को किसी Expression में किस दिशा में Group किया जाएगा। Java में ऑपरेटर सामान्यतः Left-to-Right या Right-to-Left Associativity का पालन करते हैं।
1. Left-to-Right Associativity (बाएँ से दाएँ साहचर्यता)
Most binary operators, such as arithmetic, relational, equality, and bitwise operators, are grouped from left to right.
अधिकांश Binary Operators, जैसे Arithmetic, Relational, Equality और Bitwise Operators, बाएँ से दाएँ Group किए जाते हैं।
Example:
int result = 20 - 5 - 3;System.out.println(result);
Output:
12Explanation:
(20 - 5) - 315 - 312
2. Right-to-Left Associativity (दाएँ से बाएँ साहचर्यता)
Unary operators, conditional operators, and assignment operators generally follow right-to-left associativity.
Unary Operators, Conditional Operators और Assignment Operators सामान्यतः Right-to-Left Associativity का पालन करते हैं।
Example:
int a, b, c;a = b = c = 10;System.out.println(a);System.out.println(b);System.out.println(c);
Output:
101010
Explanation:
The assignment expression is grouped as:
a = (b = (c = 10))इस Assignment Expression को दाईं से बाईं ओर Group किया जाता है।
Difference Between Operator Precedence and Associativity (Precedence और Associativity में अंतर)
| Operator Precedence | Operator Associativity |
|---|---|
| Determines which operator has higher priority. | Determines the grouping direction of operators having the same precedence. |
| Determines the order between operators of different precedence levels. | Determines grouping when operators have equal precedence. |
Example: 10 + 5 * 2 | Example: 20 - 5 - 3 |
| Multiplication is grouped before addition. | Subtraction is grouped from left to right. |
हिंदी में अंतर:
| Operator Precedence | Operator Associativity |
|---|---|
| यह निर्धारित करता है कि किस ऑपरेटर की प्राथमिकता अधिक है। | यह समान प्राथमिकता वाले ऑपरेटरों की Grouping की दिशा निर्धारित करता है। |
| अलग-अलग प्राथमिकता वाले ऑपरेटरों के बीच क्रम निर्धारित करता है। | समान प्राथमिकता वाले ऑपरेटरों के लिए Grouping निर्धारित करता है। |
उदाहरण: 10 + 5 * 2 | उदाहरण: 20 - 5 - 3 |
| इसमें गुणा पहले किया जाता है। | इसमें घटाव बाएँ से दाएँ किया जाता है। |
Use of Parentheses in Operator Precedence (ऑपरेटर प्राथमिकता में Parentheses का उपयोग)
Parentheses () are used to change the normal order of evaluation. Expressions inside parentheses are evaluated before the surrounding operators, subject to Java's expression evaluation rules.
Parentheses () का उपयोग मूल्यांकन के सामान्य क्रम को बदलने के लिए किया जाता है। Parentheses के अंदर दिए गए Expression का मूल्यांकन उसके आसपास के Operators से पहले किया जाता है, Java के Expression Evaluation नियमों के अनुसार।
Example 1: Without Parentheses
int result = 10 + 5 * 2;System.out.println(result);
Output:
20Example 2: With Parentheses
int result = (10 + 5) * 2;System.out.println(result);
Output:
30In the first example, multiplication is performed before addition. In the second example, parentheses force addition to be performed first.
पहले उदाहरण में गुणा पहले किया जाता है और उसके बाद जोड़ किया जाता है। दूसरे उदाहरण में Parentheses के कारण जोड़ पहले किया जाता है।
Operator precedence is an important concept in Java programming. It determines the order in which operators are grouped and evaluated in an expression. Associativity helps determine the grouping direction when operators have the same precedence. Understanding both concepts helps programmers avoid errors and write clear, predictable expressions. For better readability, programmers should use parentheses () whenever an expression is complex or its evaluation order may be confusing.
Operator Precedence जावा प्रोग्रामिंग का एक महत्वपूर्ण सिद्धांत है। यह निर्धारित करता है कि किसी Expression में ऑपरेटरों का Grouping और मूल्यांकन किस क्रम में होगा। जब ऑपरेटरों की प्राथमिकता समान होती है, तब Associativity उनकी Grouping की दिशा निर्धारित करती है। इन दोनों अवधारणाओं को समझने से प्रोग्रामर त्रुटियों से बच सकते हैं और स्पष्ट Expressions लिख सकते हैं। अधिक स्पष्टता के लिए जटिल Expressions में Parentheses () का उपयोग करना चाहिए, विशेषकर तब जब मूल्यांकन का क्रम समझने में कठिनाई हो सकती है।
Comments
Post a Comment