Boolean Wrapper Class in Java

The Boolean class in Java is a wrapper class for the primitive data type boolean. It is part of the java.lang package and provides methods to work with boolean values, such as parsing, converting, and comparing them. The Boolean class also provides constants for representing the boolean values true and false.

Key Features of Boolean Wrapper Class

  1. Immutability: Instances of the Boolean class are immutable.
  2. Constants: Provides constants like TRUE and FALSE.
  3. Static Methods: Methods for parsing strings to booleans, comparing booleans, and converting booleans to strings.

Boolean Class Methods

Here are some commonly used methods in the Boolean class:

  1. boolean booleanValue()
  2. int compareTo(Boolean b)
  3. static int compare(boolean x, boolean y)
  4. boolean equals(Object obj)
  5. static boolean getBoolean(String name)
  6. int hashCode()
  7. static boolean parseBoolean(String s)
  8. String toString()
  9. static String toString(boolean b)
  10. static Boolean valueOf(boolean b)
  11. static Boolean valueOf(String s)

1. boolean booleanValue()

Returns the value of this Boolean as a boolean.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b = Boolean.TRUE;
        boolean value = b.booleanValue();
        System.out.println(value);  // Output: true
    }
}

Output:

true

2. int compareTo(Boolean b)

Compares this Boolean object with the specified Boolean object.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b1 = Boolean.TRUE;
        Boolean b2 = Boolean.FALSE;
        System.out.println(b1.compareTo(b2));  // Output: 1
    }
}

Output:

1

3. static int compare(boolean x, boolean y)

Compares two boolean values.

public class BooleanExample {
    public static void main(String[] args) {
        System.out.println(Boolean.compare(true, false));  // Output: 1
    }
}

Output:

1

4. boolean equals(Object obj)

Compares this object to the specified object.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b1 = Boolean.TRUE;
        Boolean b2 = Boolean.TRUE;
        System.out.println(b1.equals(b2));  // Output: true
    }
}

Output:

true

5. static boolean getBoolean(String name)

Returns true if and only if the system property with the specified name exists and is equal to the string "true".

public class BooleanExample {
    public static void main(String[] args) {
        System.setProperty("myBoolean", "true");
        boolean b = Boolean.getBoolean("myBoolean");
        System.out.println(b);  // Output: true
    }
}

Output:

true

6. int hashCode()

Returns a hash code for this Boolean object.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b = Boolean.TRUE;
        System.out.println(b.hashCode());  // Output: 1231
    }
}

Output:

1231

7. static boolean parseBoolean(String s)

Parses the string argument as a boolean.

public class BooleanExample {
    public static void main(String[] args) {
        boolean b = Boolean.parseBoolean("true");
        System.out.println(b);  // Output: true
    }
}

Output:

true

8. String toString()

Returns a string representation of this Boolean object.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b = Boolean.TRUE;
        System.out.println(b.toString());  // Output: "true"
    }
}

Output:

true

9. static String toString(boolean b)

Returns a String object representing the specified boolean.

public class BooleanExample {
    public static void main(String[] args) {
        String s = Boolean.toString(true);
        System.out.println(s);  // Output: "true"
    }
}

Output:

true

10. static Boolean valueOf(boolean b)

Returns a Boolean instance representing the specified boolean value.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b = Boolean.valueOf(true);
        System.out.println(b);  // Output: true
    }
}

Output:

true

11. static Boolean valueOf(String s)

Returns a Boolean with a value represented by the specified string.

public class BooleanExample {
    public static void main(String[] args) {
        Boolean b = Boolean.valueOf("true");
        System.out.println(b);  // Output: true
    }
}

Output:

true

Conclusion

The Boolean class in Java provides a wide range of methods for manipulating and converting boolean values. Understanding and utilizing these methods can greatly enhance your ability to handle boolean operations in your applications.

Comments