Java के Booleans
In this page:
boolean variableName = true; // or false
boolean result = a > b;
The boolean Type
boolean type दो possible values में से बिल्कुल एक represent करता है, true या false, और conceptually Java का सबसे छोटा data type है, जब भी information का एक टुकड़ा एक साधारण yes-or-no या on-or-off flag हो उपयोग होता है।
उदाहरण: The boolean Type
public class Main {
public static void main(String[] args) {
boolean isOpen = true; // simple yes/no flag
System.out.println(isOpen);
}
}
Login to try C/C++/Java/PHP code in the editor
Boolean Expressions
एक boolean expression कोई भी expression है जो true या false तक evaluate होता है, आमतौर पर सीधे एक literal true या false लिखने के बजाय >, <, ==, या != जैसे operators से दो values compare करके बना।
उदाहरण: Boolean Expressions
public class Main {
public static void main(String[] args) {
boolean isAdult = 20 > 18; // comparison, not a literal true/false
System.out.println(isAdult);
}
}
Login to try C/C++/Java/PHP code in the editor
Booleans in Conditions
Boolean values वे हैं जो if, while, और for जैसे control flow statements असल में test करते हैं: एक if statement अपना block सिर्फ तब चलाता है जब इसके parentheses के अंदर boolean expression true evaluate होता है।
उदाहरण: Booleans in Conditions
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare loggedIn and set it to true
boolean loggedIn = true;
// Check whether loggedIn
if (loggedIn) {
// Print a line to the console
System.out.println("Runs only because loggedIn is true");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Boolean Variables from Comparisons
एक comparison का result inline comparison दोहराने के बजाय एक named boolean variable में store करना conditions को पढ़ना आसान बनाता है और वही computed result कई जगहों पर reuse होने देता है।
उदाहरण: Boolean Variables from Comparisons
public class Main {
public static void main(String[] args) {
int score = 75;
boolean passed = score >= 60; // named result, reused below
System.out.println(passed);
System.out.println("Result: " + passed);
}
}
Login to try C/C++/Java/PHP code in the editor
Default Boolean Value
एक uninitialized boolean instance या static field अपने आप false default करता है, और एक नई बनाई boolean array का हर element भी तब तक false से शुरू होता है जब तक एक value explicitly assign न की जाए।
उदाहरण: Default Boolean Value
public class Main {
static boolean flag; // uninitialized instance field
public static void main(String[] args) {
boolean[] flags = new boolean[3];
System.out.println(flag); // defaults to false
System.out.println(flags[0]); // array elements default to false too
}
}
Login to try C/C++/Java/PHP code in the editor
if (x == true)के बजायif (x = true)लिखना, या बेहतरif (x), जो compare करने के बजाय assign करता है।- C की तरह
0या1को booleans के रूप में उपयोग करना, जो Java में compile नहीं होता। - Capital या quotes के साथ
Trueया"true"लिखना, जब literalstrueऔरfalseहैं।
- Java को इसकी history, features, environment setup, और इसके basic syntax और structure वाले एक पहले program के साथ introduce किया गया है।
- Comments, keywords, और identifiers Java code के basic building blocks हैं।
- Data types,
var, variables, constants, और booleans define करते हैं कि values कैसे store और उपयोग होती हैं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: