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 CodeOutput:
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 CodeOutput:
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 CodeOutput:
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 CodeOutput:
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: