← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 12 of 12

Java के Booleans

boolean type बिल्कुल दो values में से एक store करता है, true या false, और Java program में हर conditional statement और loop के decisions drive करता है।
Syntax
java
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

java
public class Main {
	public static void main(String[] args) {
		boolean isOpen = true; // simple yes/no flag
		System.out.println(isOpen);
	}
}

Boolean Expressions

एक boolean expression कोई भी expression है जो true या false तक evaluate होता है, आमतौर पर सीधे एक literal true या false लिखने के बजाय >, <, ==, या != जैसे operators से दो values compare करके बना।

उदाहरण: Boolean Expressions

java
public class Main {
	public static void main(String[] args) {
		boolean isAdult = 20 > 18; // comparison, not a literal true/false
		System.out.println(isAdult);
	}
}

Booleans in Conditions

Boolean values वे हैं जो if, while, और for जैसे control flow statements असल में test करते हैं: एक if statement अपना block सिर्फ तब चलाता है जब इसके parentheses के अंदर boolean expression true evaluate होता है।

उदाहरण: Booleans in Conditions

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

Boolean Variables from Comparisons

एक comparison का result inline comparison दोहराने के बजाय एक named boolean variable में store करना conditions को पढ़ना आसान बनाता है और वही computed result कई जगहों पर reuse होने देता है।

उदाहरण: Boolean Variables from Comparisons

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

Default Boolean Value

एक uninitialized boolean instance या static field अपने आप false default करता है, और एक नई बनाई boolean array का हर element भी तब तक false से शुरू होता है जब तक एक value explicitly assign न की जाए।

उदाहरण: Default Boolean Value

java
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
	}
}
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. if (x == true) के बजाय if (x = true) लिखना, या बेहतर if (x), जो compare करने के बजाय assign करता है।
  2. C की तरह 0 या 1 को booleans के रूप में उपयोग करना, जो Java में compile नहीं होता।
  3. Capital या quotes के साथ True या "true" लिखना, जब literals true और 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 और उपयोग होती हैं।

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.