Java Boolean Class

Introduction

The Boolean class in Java is a wrapper for the primitive type boolean. It provides utility methods for manipulating boolean values and converting between boolean and String.

Table of Contents

  1. What is Boolean?
  2. Creating Boolean Instances
  3. Common Methods
  4. Examples of Boolean
  5. Conclusion

1. What is Boolean?

Boolean is a final class that wraps a value of the primitive type boolean. It provides methods for converting boolean values and performing logical operations.

2. Creating Boolean Instances

You can create Boolean instances in two main ways:

  • Using the Boolean constructor: new Boolean(boolean value) or new Boolean(String value)
  • Using the Boolean.valueOf(boolean value) or Boolean.valueOf(String value) methods

3. Common Methods

  • booleanValue(): Returns the value of this Boolean object as a primitive boolean.
  • toString(): Returns a String representation of the boolean value.
  • parseBoolean(String s): Parses a String to a primitive boolean.
  • compare(Boolean b1, Boolean b2): Compares two Boolean objects.
  • logicalAnd(boolean a, boolean b): Returns the logical AND of two boolean values.
  • logicalOr(boolean a, boolean b): Returns the logical OR of two boolean values.
  • logicalXor(boolean a, boolean b): Returns the logical XOR of two boolean values.

4. Examples of Boolean

Example 1: Creating Boolean Instances

This example demonstrates how to create Boolean instances using constructors and valueOf methods.

public class BooleanInstanceExample {
    public static void main(String[] args) {
        Boolean b1 = new Boolean(true);
        Boolean b2 = Boolean.valueOf("true");
        Boolean b3 = Boolean.valueOf(false);

        System.out.println("b1: " + b1);
        System.out.println("b2: " + b2);
        System.out.println("b3: " + b3);
    }
}

Output:

b1: true
b2: true
b3: false

Example 2: Parsing a String to boolean

This example shows how to parse a String to a primitive boolean using parseBoolean.

public class ParseBooleanExample {
    public static void main(String[] args) {
        boolean b1 = Boolean.parseBoolean("true");
        boolean b2 = Boolean.parseBoolean("false");

        System.out.println("b1: " + b1);
        System.out.println("b2: " + b2);
    }
}

Output:

b1: true
b2: false

Example 3: Logical Operations

In this example, we demonstrate logical operations using logicalAnd, logicalOr, and logicalXor.

public class LogicalOperationsExample {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;

        System.out.println("Logical AND: " + Boolean.logicalAnd(a, b));
        System.out.println("Logical OR: " + Boolean.logicalOr(a, b));
        System.out.println("Logical XOR: " + Boolean.logicalXor(a, b));
    }
}

Output:

Logical AND: false
Logical OR: true
Logical XOR: true

Conclusion

The Boolean class in Java is a useful wrapper for the primitive boolean type. It provides methods for manipulating boolean values, parsing strings, and performing logical operations, making it a versatile tool in Java programming.

Leave a Comment

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

Scroll to Top