Java Boolean का उपयोग true या false में परिणाम देने के लिए किया जाता है, programming में कभी कभी हमें इस तरह के data type की आवश्यकता होती है जो दो में से एक की वैल्यू को स्टोर करता है।
Structure के आधार पर boolean को दो भागों में बाँटा जा सकता है।
- Value.
- Expression.
Java Boolean Value
एक boolean data type, boolean
कीवर्ड के साथ असाइन किया जाता है। और boolean केवल true या false में ही वैल्यू देता है।
Example: Booleans Value
public class MyClass {
public static void main(String[] args) {
boolean sunIsHot = true;
boolean moonIsHot = false;
System.out.println(sunIsHot); // Result: true
System.out.println(moonIsHot); // Result: false
}
}
Java Boolean Expression
Boolean expression एक जावा expression है जिसका उपयोग comparison operator के लिए किया जाता है, यह भी केवल true या false में ही वैल्यू देता है। आसानी से समझने के लिए कुछ उदाहरण नीचे दिया गया है।
Example: Boolean Expression
public class MyClass {
public static void main(String[] args) {
int x = 8;
int y = 5;
System.out.println(x == y); // Result: false
System.out.println(x != y); // Result: true
System.out.println(x > y); // Result: true
System.out.println(x < y); // Result: false
System.out.println(x >= y); // Result: true
System.out.println(x <= y); // Result: false
}
}
Example: Boolean Expression बिना variable के
public class MyClass {
public static void main(String[] args) {
System.out.println(10 != 15); // Result: true
System.out.println(10 > 15); // Result: false
System.out.println(10 < 15); // Result: true
System.out.println(10 >= 15); // Result: false
System.out.println(10 <= 15); // Result: true
}
}
नोट: जावा में comparisons और conditions के लिए Boolean ही base वैल्यू होता है।