Java में Operator Precedence
In this page:
Arithmetic Precedence
बिल्कुल standard math की तरह, Java उसी expression में + और - से पहले * और / evaluate करता है, इसलिए 2 + 3 * 4 14 evaluate होता है, 20 नहीं -- multiplication left-to-right position की परवाह किए बिना पहले होता है।
उदाहरण: Arithmetic Precedence
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4); // 14, not 20 - multiplication runs first
}
}
Login to try C/C++/Java/PHP code in the editor
Relational and Logical Precedence
> या < जैसे Relational operators && या || जैसे logical operators से पहले evaluate होते हैं, यही कारण है कि a > 5 && b < 10 को सही से काम करने के लिए हर comparison के आस-पास extra parentheses की ज़रूरत नहीं।
उदाहरण: Relational and Logical Precedence
public class Main {
public static void main(String[] args) {
int a = 6, b = 5;
System.out.println(a > 5 && b < 10); // comparisons evaluated before &&, no extra parens needed
}
}
Login to try C/C++/Java/PHP code in the editor
Left-to-Right Associativity
जब दो operators identical precedence share करते हैं, जैसे एक row में दो additions, Java उन्हें left से right resolve करता है -- 10 - 3 - 2 (10 - 3) - 2 = 5 evaluate होता है, 10 - (3 - 2) नहीं।
उदाहरण: Left-to-Right Associativity
public class Main {
public static void main(String[] args) {
System.out.println(10 - 3 - 2); // (10 - 3) - 2 = 5, evaluated left to right
}
}
Login to try C/C++/Java/PHP code in the editor
Assignment Precedence
Assignment operators वह दुर्लभ exception हैं जो right से left evaluate होते हैं, जो बिल्कुल वही है जो a = b = c = 0; को इरादे के अनुसार काम कराता है -- c पहले assign होता है, फिर वह result b को assign होता है, फिर a को।
उदाहरण: Assignment Precedence
public class Main {
public static void main(String[] args) {
int a, b, c;
a = b = c = 0; // right to left: c assigned first, then b, then a
System.out.println(a + " " + b + " " + c);
}
}
Login to try C/C++/Java/PHP code in the editor
Modulo Operator Priority
% * और / जैसा बिल्कुल वही precedence tier share करता है, इसलिए 10 + 6 % 4 जैसे एक mixed expression में, modulo addition से पहले compute होता है, 10 + 2 = 12 देते हुए।
उदाहरण: Modulo Operator Priority
public class Main {
public static void main(String[] args) {
System.out.println(10 + 6 % 4); // % computed first (same tier as * and /): 10 + 2 = 12
}
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
&&और||बराबर priority रखते हैं, जब&&पहले चलता है, इसलिएa || b && ca || (b && c)है। - यह उम्मीद करना कि
-right से left evaluate होगा, जब10 - 4 - 33है क्योंकि यह left से right जाता है। - Complex expressions में parentheses उपयोग न करना।
- Java में values के साथ काम करने के लिए arithmetic, relational, logical, bitwise, और assignment operators हैं।
- Increment, decrement, और ternary operator values update करने या एक result चुनने के छोटे तरीके प्रदान करते हैं।
- Operator precedence तय करता है कि एक mixed expression कैसे evaluate होता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: