Java के Relational Operators
In this page:
boolean result = a == b; // also !=, >, <, >=, <=
Equality and Inequality
== जांचता है कि दो primitive values बिल्कुल बराबर हैं या नहीं, और != जांचता है कि वे अलग हैं या नहीं -- दोनों हमेशा एक boolean result produce करते हैं जिसे आप सीधे एक condition में उपयोग कर सकते हैं।
उदाहरण: Equality and Inequality
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
}
}
Login to try C/C++/Java/PHP code in the editor
Greater Than and Less Than
> और < दो numeric values के बीच magnitude compare करते हैं और accordingly true या false return करते हैं; वे सिर्फ primitives पर काम करते हैं, String जैसे objects पर नहीं, जिनका एक natural numeric ordering नहीं है।
उदाहरण: Greater Than and Less Than
public class Main {
public static void main(String[] args) {
System.out.println(7 > 3); // true
System.out.println(7 < 3); // false
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
Comparing Objects
Objects के लिए, == compare करता है कि दो variables memory में बिल्कुल उसी object की ओर point करते हैं या नहीं, उनकी contents match करती हैं या नहीं नहीं -- "hello" रखने वाले दो अलग String objects == से unequal compare होंगे, इसलिए उनके असली characters compare करने के लिए आपको .equals() चाहिए।
उदाहरण: Comparing Objects
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Strings को
==से compare करना, जो references compare करता है;equals()उपयोग करें। - Values compare करते समय
==के बजाय=उपयोग करना, जो assign करता है और अक्सर conditions में compile नहीं होता। doublevalues को==से compare करना, जहां rounding बराबर दिखने वाली values को unequal बना सकता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: