Explain Boolean in Java with suitable example

Boolean in Java

Boolean in Java: A Boolean in Java is a primitive data type that can hold one of two values: true or false. It is used to represent true/false conditions and is often used in control flow statements such as if, while, and for.

Declaring a Boolean in Java

You can declare a Boolean variable like this:

boolean isJavaFun = true;
boolean isFishTasty = false;

Using Boolean in Java

Booleans are typically used in conditional statements to control the flow of the program. Here are some examples:

If Statement

boolean isRaining = true;

if (isRaining) {
    System.out.println("It's raining. Take an umbrella!");
} else {
    System.out.println("It's not raining. You don't need an umbrella.");
}

While Loop

int i = 1;
boolean keepCounting = true;

while (keepCounting) {
    System.out.println("Count: " + i);
    i++;
    if (i > 5) {
        keepCounting = false;
    }
}

For Loop

for (boolean keepRunning = true; keepRunning; ) {
    System.out.println("Running...");
    // some condition to stop the loop
    keepRunning = false;
}

Logical Operators

Java provides several logical operators that you can use with Boolean values:

  • && (logical AND): Returns true if both operands are true.
  • || (logical OR): Returns true if at least one operand is true.
  • ! (logical NOT): Inverts the value of a Boolean.

Examples:

boolean a = true;
boolean b = false;

boolean result1 = a && b; // false
boolean result2 = a || b; // true
boolean result3 = !a;     // false
boolean result4 = !b;     // true

Comparison Operators

Booleans are often the result of comparison operations:

  • == (equal to)
  • != (not equal to)
  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Example:

int x = 10;
int y = 20;

boolean isEqual = (x == y);    // false
boolean isNotEqual = (x != y); // true
boolean isLess = (x < y);      // true
boolean isGreater = (x > y);   // false
boolean isLessOrEqual = (x <= y);  // true
boolean isGreaterOrEqual = (x >= y); // false

Boolean Wrapper Class

Java also provides a wrapper class for the primitive Boolean type, which is Boolean. This is useful when you need to work with objects rather than primitive types.

Boolean isJavaFun = Boolean.TRUE;
Boolean isFishTasty = Boolean.FALSE;

System.out.println(isJavaFun);    // true
System.out.println(isFishTasty);  // false

Autoboxing and Unboxing

Java allows automatic conversion between the primitive boolean and the Boolean wrapper class. This feature is known as autoboxing and unboxing.

Boolean isJavaFun = true;  // Autoboxing
boolean isFishTasty = Boolean.FALSE;  // Unboxing

Conclusion

Booleans are a fundamental part of Java programming, used to make decisions and control the flow of the program. Understanding how to use Booleans and logical operators will help you write more efficient and readable code.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *