← Back to Core Java Course | Chapter 3: Operators | Lesson 9 of 9

Java में Operator Precedence

Operator precedence वह order है जो Java follow करता है जब एक math या logic line में कई operators हों, school में addition से पहले multiplication करने जैसा। Brackets आपको order खुद चुनने देते हैं।

Arithmetic Precedence

बिल्कुल standard math की तरह, Java उसी expression में + और - से पहले * और / evaluate करता है, इसलिए 2 + 3 * 4 14 evaluate होता है, 20 नहीं -- multiplication left-to-right position की परवाह किए बिना पहले होता है।

उदाहरण: Arithmetic Precedence

java
public class Main {
	public static void main(String[] args) {
		System.out.println(2 + 3 * 4); // 14, not 20 - multiplication runs first
	}
}

Relational and Logical Precedence

> या < जैसे Relational operators && या || जैसे logical operators से पहले evaluate होते हैं, यही कारण है कि a > 5 && b < 10 को सही से काम करने के लिए हर comparison के आस-पास extra parentheses की ज़रूरत नहीं।

उदाहरण: Relational and Logical Precedence

java
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
	}
}

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

java
public class Main {
	public static void main(String[] args) {
		System.out.println(10 - 3 - 2); // (10 - 3) - 2 = 5, evaluated left to right
	}
}

Assignment Precedence

Assignment operators वह दुर्लभ exception हैं जो right से left evaluate होते हैं, जो बिल्कुल वही है जो a = b = c = 0; को इरादे के अनुसार काम कराता है -- c पहले assign होता है, फिर वह result b को assign होता है, फिर a को।

उदाहरण: Assignment Precedence

java
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);
	}
}

Modulo Operator Priority

% * और / जैसा बिल्कुल वही precedence tier share करता है, इसलिए 10 + 6 % 4 जैसे एक mixed expression में, modulo addition से पहले compute होता है, 10 + 2 = 12 देते हुए।

उदाहरण: Modulo Operator Priority

java
public class Main {
	public static void main(String[] args) {
		System.out.println(10 + 6 % 4); // % computed first (same tier as * and /): 10 + 2 = 12
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि && और || बराबर priority रखते हैं, जब && पहले चलता है, इसलिए a || b && c a || (b && c) है।
  2. यह उम्मीद करना कि - right से left evaluate होगा, जब 10 - 4 - 3 3 है क्योंकि यह left से right जाता है।
  3. Complex expressions में parentheses उपयोग न करना।
चैप्टर सारांश
  • Java में values के साथ काम करने के लिए arithmetic, relational, logical, bitwise, और assignment operators हैं।
  • Increment, decrement, और ternary operator values update करने या एक result चुनने के छोटे तरीके प्रदान करते हैं।
  • Operator precedence तय करता है कि एक mixed expression कैसे evaluate होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.