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

Java के Relational Operators

Relational operators दो values compare करते हैं, जैसे पूछना कौन लंबा है। Answer हमेशा true या false होता है।
Syntax
java
boolean result = a == b;  // also !=, >, <, >=, <=

Equality and Inequality

== जांचता है कि दो primitive values बिल्कुल बराबर हैं या नहीं, और != जांचता है कि वे अलग हैं या नहीं -- दोनों हमेशा एक boolean result produce करते हैं जिसे आप सीधे एक condition में उपयोग कर सकते हैं।

उदाहरण: Equality and Inequality

java
public class Main {
	public static void main(String[] args) {
		int a = 5, b = 5;
		System.out.println(a == b); // true
		System.out.println(a != b); // false
	}
}

Greater Than and Less Than

> और < दो numeric values के बीच magnitude compare करते हैं और accordingly true या false return करते हैं; वे सिर्फ primitives पर काम करते हैं, String जैसे objects पर नहीं, जिनका एक natural numeric ordering नहीं है।

उदाहरण: Greater Than and Less Than

java
public class Main {
	public static void main(String[] args) {
		System.out.println(7 > 3);  // true
		System.out.println(7 < 3);  // false
	}
}

Greater Than or Equal to and Less Than or Equal to

>= और <= अपने strict counterparts जैसे काम करते हैं लेकिन दोनों values बिल्कुल बराबर होने पर भी true return करते हैं, जो boundary conditions के लिए मायने रखता है जैसे यह जांचना कि एक age 18 या उससे ज़्यादा qualify करती है या नहीं।

उदाहरण: Greater Than or Equal to and Less Than or Equal to

java
public class Main {
	public static void main(String[] args) {
		int age = 18;
		System.out.println(age >= 18); // true - boundary included
		System.out.println(age <= 17); // false
	}
}

Comparing Objects

Objects के लिए, == compare करता है कि दो variables memory में बिल्कुल उसी object की ओर point करते हैं या नहीं, उनकी contents match करती हैं या नहीं नहीं -- "hello" रखने वाले दो अलग String objects == से unequal compare होंगे, इसलिए उनके असली characters compare करने के लिए आपको .equals() चाहिए।

उदाहरण: Comparing Objects

java
public class Main {
	public static void main(String[] args) {
		String a = new String("hello");
		String b = new String("hello");
		System.out.println(a == b);      // false - different objects in memory
		System.out.println(a.equals(b)); // true - same content
	}
}

Using Relational Operators in Conditions

Relational operators पहली जगह एक if statement की condition को इसकी boolean value देते हैं, इसलिए उन्हें logical operators (&&, ||) के साथ combine करना multi-part decision logic express करने का तरीका है।

उदाहरण: Using Relational Operators in Conditions

java
public class Main {
	public static void main(String[] args) {
		int age = 20;
		int score = 85;
		if (age >= 18 && score > 80) { // relational + logical combined
			System.out.println("Eligible");
		}
	}
}
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. Strings को == से compare करना, जो references compare करता है; equals() उपयोग करें।
  2. Values compare करते समय == के बजाय = उपयोग करना, जो assign करता है और अक्सर conditions में compile नहीं होता।
  3. double values को == से compare करना, जहां rounding बराबर दिखने वाली values को unequal बना सकता है।

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.