Header Ads

Booleans in Java

  Booleans in Java

In Java, booleans are a primitive data type that represents a binary value: either true or false. They are used to store and evaluate logical values, making them essential for decision-making in programming. Booleans are primarily employed in conditional statements, loops, and other scenarios where a binary choice needs to be made.

In Java, the boolean data type is denoted by the keyword "boolean." It has two possible values: "true" and "false." These values are case-sensitive and cannot be represented by any other literal. The size of a boolean variable is not explicitly defined in Java, but it typically takes up one byte of memory.

Here's an example of how to declare and use boolean variables in Java:


public class BooleanExample {

    public static void main(String[] args) {

        boolean isSunny = true;

        boolean isRaining = false;


        if (isSunny) {

            System.out.println("It's a sunny day!");

        } else if (isRaining) {

            System.out.println("It's raining outside.");

        } else {

            System.out.println("The weather is unclear.");

        }


        boolean hasPassedExam = true;

        boolean hasCompletedAssignments = false;


        if (hasPassedExam && hasCompletedAssignments) {

            System.out.println("Congratulations! You passed the exam and completed all assignments.");

        } else if (hasPassedExam || hasCompletedAssignments) {

            System.out.println("You either passed the exam or completed all assignments.");

        } else {

            System.out.println("You need to work hard on both the exam and assignments.");

        }

    }

}


In this example, we declared and initialized four boolean variables: `isSunny`, `isRaining`, `hasPassedExam`, and `hasCompletedAssignments`. We then used them in conditional statements to print messages based on their values.

Booleans are the building blocks of logic in Java and play a crucial role in controlling the flow of programs and making decisions based on conditions.


हिंदी:

जावा में, बूलियन एक प्राथमिक डेटा प्रकार है जो एक द्विआधारिक मान को प्रतिष्ठित करता है: सत्य (true) या असत्य (false)। इन्हें संज्ञानात्मक निर्णयों में संग्रहीत और मूल्यांकन करने के लिए उपयोग किया जाता है, जो प्रोग्रामिंग में निर्णय लेने के लिए महत्वपूर्ण होते हैं। बूलियन प्राथमिक रूप से शर्ताधारी वाक्यों, लूप्स, और अन्य स्थितियों में उपयोग किए जाते हैं जहां द्विआधारिक विकल्प की आवश्यकता होती है।

जावा में, बूलियन डेटा प्रकार को "boolean" की कीवर्ड से दर्शाया जाता है। इसके दो संभावित मान होते हैं: "true" और "false"। ये मान केस-संवेदी होते हैं और किसी अन्य लिटरल द्वारा प्रतिष्ठित नहीं किए जा सकते। एक बूलियन चर का आकार जावा में निर्दिष्ट नहीं होता है, लेकिन आमतौर पर इसकी स्मृति में एक बाइट मेमोरी का उपयोग किया जाता है।

यहां एक उदाहरण है जिसमें जावा में बूलियन चरों की घोषणा करने और उनका उपयोग करने का तरी का दिखाया गया है:


public class BooleanExample {

    public static void main(String[] args) {

        boolean isSunny = true;

        boolean isRaining = false;


        if (isSunny) {

            System.out.println("यह सूर्यमय दिन है!");

        } else if (isRaining) {

            System.out.println("बाहर बारिश हो रही है।");

        } else {

            System.out.println("मौसम अस्पष्ट है।");

        }


        boolean hasPassedExam = true;

        boolean hasCompletedAssignments = false;


        if (hasPassedExam && hasCompletedAssignments) {

            System.out.println("बधाई हो! आपने परीक्षा पास की है और सभी असाइनमेंट पूरे किए हैं।");

        } else if (hasPassedExam || hasCompletedAssignments) {

            System.out.println("आपने या तो परीक्षा पास की है या सभी असाइनमेंट पूरे किए हैं।");

        } else {

            System.out.println("आपको परीक्षा और असाइनमेंट दोनों पर मेहनत करनी होगी।");

        }

    }

}

इस उदाहरण में, हमने चार बूलियन चरों को घोषित और प्रारंभिकीकृत किया है: `isSunny`, `isRaining`, `hasPassedExam`, और `hasCompletedAssignments`। हमने फिर इन्हें शर्ताधारी वाक्यों में उपयोग करके उनके मानों के आधार पर संदेशों को प्रिंट करने के लिए उपयोग किया है।

बूलियन जावा में तर्क के निर्माण के लिए महत्वपूर्ण हैं और कार्यक्रमों को नियंत्रित करने और शर्तों के आधार पर निर्णय लेने में महत्वपूर्ण भूमिका निभाते हैं।

No comments

Powered by Blogger.