Table of contents
1.
Introduction
2.
Creation of Boolean Class Objects
3.
Fields of Java Boolean class 
4.
Methods of boolean type
4.1.
1. static boolean parseBoolean(String s)
4.2.
2. boolean booleanValue()
4.3.
3. static Boolean valueOf(boolean b)
4.4.
4. static Boolean valueOf(String s)
4.5.
5. static String toString(boolean b)
4.6.
6. String toString()
4.7.
7. int hashCode()
4.8.
8. boolean equals(Object obj)
4.9.
9. int compareTo(Boolean b)
4.10.
10. int compare(boolean x, boolean y)
5.
Examples of Java boolean Keyword
5.1.
Example 1: Simple Boolean Declaration and Assignment
5.2.
Example 2: Boolean in Conditional Statements
5.3.
Example 3: Boolean with Logical Operations
5.4.
Example 4: Boolean in Loops
6.
Frequently Asked Questions
6.1.
What is the default value of a boolean in Java?
6.2.
Can you perform arithmetic operations on booleans?
6.3.
Can you use booleans in switch statements?
6.4.
What is the purpose of the Boolean wrapper class methods?
7.
Conclusion
Last Updated: Dec 6, 2024
Easy

Boolean Class in Java

Author Pankhuri Goel
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, the boolean keyword is a primitive data type. It can only store true or false as a potential value. It specifies a single bit of data, and its “size” is hard to determine.

The boolean keyword is used with variables and methods. The default value is false. It is most commonly used with conditional sentences.

In java.lang package, there is a wrapper class called Boolean. A value of the primitive type boolean is wrapped in an object by the Boolean class. A single field of type boolean is contained in a Boolean object.

In addition, this class contains handy methods for dealing with boolean variables, such as converting a boolean to a String and a String to a boolean.

Boolean Class in Java

Creation of Boolean Class Objects

For creating Boolean objects, the Boolean class provides two constructors.

The statement below creates a Boolean object that includes the value argument.

Boolean b = new Boolean(boolean value)

If the string parameter is not null and equals, ignoring the case, the string “true,” a Boolean object with the value true is generated; otherwise, a Boolean object with the value false is constructed. Shown below is the representation of the second type of constructor.

Boolean b = new Boolean(String s);

Fields of Java Boolean class 

The fields of the Boolean class are listed below.

  • static boolean FALSE is a static Boolean object that corresponds to the primitive value false.
  • static boolean TRUE is a static Boolean object that corresponds to the primitive value true.
  • static class is the Class object that represents the primitive type boolean.

Methods of boolean type

Let’s see the various methods available in the Boolean class.

1. static boolean parseBoolean(String s)

The string argument is parsed as a boolean by this function. The boolean result returned is true if the string argument is not null and equals the string “true,” regardless of the case; otherwise, return false.

Syntax: 

public static boolean parseBoolean(String s)

where s is the string containing the to-be-parsed boolean representation argument.

This method returns the boolean represented by the string.

2. boolean booleanValue()

This function returns the value of the Boolean object as a boolean primitive.

Syntax: 

public boolean booleanValue()

This object’s primitive boolean value is returned.

3. static Boolean valueOf(boolean b)

The provided boolean value is represented by a Boolean object returned by this method. This method returns Boolean TRUE if the supplied boolean value is true and Boolean FALSE if it is false.

Syntax: 

public static boolean valueOf(boolean b)

where b is a boolean value.

This method returns a Boolean object that has the value b.

4. static Boolean valueOf(String s)

This method returns a Boolean with the provided string ‘s’ as its value. If the string input is not null and equals the string “true,” regardless of the case, the Boolean result returned is true.

Syntax: 

public static boolean valueOf(String s)

where s is a string.

This method returns a Boolean value which represents the string.

5. static String toString(boolean b)

The provided boolean is represented as a String object returned by this method. The string “true” will be returned if the supplied boolean is true; else, the string “false” will be returned. 

Syntax: 

public static String toString(boolean b)

where b is the boolean that has to be converted.

The string depiction of the provided boolean is returned.

6. String toString()

This method returns a String object that represents the value of the Boolean. A string equal to “true” is returned if this object represents the value true. If this is not the case, the string “false” is returned.

Syntax: 

public String toString()

This object’s string representation is returned by the method. To check this syntax, try it on online java compiler.

7. int hashCode()

For the Boolean object, this function returns a hash code. The hashcode for true is 1231, while the one for false is 1237.

Syntax: 

pubic int hashCode()

If this object represents true, it returns the integer 1231; if this object represents false, it returns the value 1237.

8. boolean equals(Object obj)

If the argument is not null and is a Boolean object with the same boolean value as this object, this method returns true.

public boolean equals(Object obj)

where obj is the object to be compared with.

If the Boolean objects indicate the same value, then true; otherwise, false.

9. int compareTo(Boolean b)

This method compares the supplied argument ‘b’ with the Boolean instance.

Syntax: 

public int compareTo(Boolean b)

where b is the Boolean instance to be compared.

The method returns zero if this object and the argument have the same boolean value. A positive value if this object is true and the argument is false; and a negative value if this object is false and the argument is true.

10. int compare(boolean x, boolean y)

This function compares primitives boolean variables.

Syntax: 

public static int compare(boolean x, boolean y)

where x is the first boolean to be compared and

y is the second boolean.

If x and y have the same boolean value, zero is returned. If x is true and y is false, the value is positive, whereas if x is false and y is true, the value is negative.

Examples of Java boolean Keyword

The boolean keyword in Java is used to declare variables that can only hold true or false values. Here are some examples:

Example 1: Simple Boolean Declaration and Assignment

public class BooleanClass{
    public static void main(String[] args) {
        boolean isJavaFun = true;
        boolean isFishTasty = false;

        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Is fish tasty? " + isFishTasty);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Is Java fun? true  
Is fish tasty? false  

Example 2: Boolean in Conditional Statements

public class BooleanClass{
    public static void main(String[] args) {
        boolean isSunny = true;

        if (isSunny) {
            System.out.println("It's a sunny day!");
        } else {
            System.out.println("It's cloudy or rainy.");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

It's a sunny day!

Example 3: Boolean with Logical Operations

public class BooleanClass{
    public static void main(String[] args) {
        boolean hasTicket = true;
        boolean isAdult = false;

        boolean canEnter = hasTicket && isAdult;
        System.out.println("Can enter the event: " + canEnter);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Can enter the event: false

Example 4: Boolean in Loops

public class BooleanClass{
    public static void main(String[] args) {
        boolean keepGoing = true;
        int count = 0;

        while (keepGoing) {
            System.out.println("Count: " + count);
            count++;
            if (count == 5) {
                keepGoing = false;
            }
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output:

Count: 0  
Count: 1  
Count: 2  
Count: 3  
Count: 4  

Frequently Asked Questions

What is the default value of a boolean in Java?

The default value of a boolean in Java is false. If a boolean field or array element is not explicitly initialized, it will automatically be set to false.

Can you perform arithmetic operations on booleans?

No, arithmetic operations cannot be performed on booleans in Java. Booleans represent logical values (true or false) and are not compatible with mathematical operations like addition, subtraction, or multiplication.

Can you use booleans in switch statements?

No, booleans cannot be used in switch statements in Java. The switch statement requires an int, char, byte, short, String, or an enumerated type as the expression.

What is the purpose of the Boolean wrapper class methods?

The Boolean wrapper class provides methods for parsing strings into boolean values (parseBoolean), comparing boolean values (compare), and converting boolean values to strings (toString) or primitives (booleanValue). It supports working with boolean data as objects.

Conclusion

In this article, we learned about the Boolean class in Java. The Boolean class in Java is a vital part of the Java programming language, providing a way to work with boolean values as objects. It simplifies handling logical data through its methods, such as parseBoolean, compare, and toString, offering more functionality than primitive boolean types. Understanding its features and usage enables developers to write cleaner, more efficient, and logically robust code.

Recommended Articles:

Live masterclass