What is the Boolean Class in Java and how to use it?

Published on Oct 11,2019 5.4K Views

What is the Boolean Class in Java and how to use it?

edureka.co

I’m pretty sure you must have come across the term Boolean. Many of you would be aware of the usage as well. So, this article on Boolean class in Java will help you learn about the working of this class and also a few more topics around it.

I’ll be discussing these topics in detail:

Let’s begin!

What is a Boolean Class in Java?

Java.lang.package offers a wrapper class Boolean in Java. The Boolean Class wraps the value of the primitive type Boolean in an object. This Class helps to provide methods that convert Boolean into string and string into Boolean while working with a Boolean variable. The question is how do we create a Boolean object? Well, this Class provides us two constructors to accomplish our goal.

Let us see how!

Constructors in Boolean Class

There are two constructors in the Boolean Class :

Boolean b = new Boolean (boolean value);

This constructor creates the Boolean object that passes a Boolean value.

Boolean b = new Boolean (String s);

This constructor helps in creating a Boolean object, that creates the value true if the string argument is not null and is equal.

Moving on, let’s take a look at the fields that Boolean Class offers!

Fields 

static Boolean TRUE: The Boolean object referring to the primitive value true.
static Boolean FALSE: The Boolean object referring to the primitive value false.
static Class: The Class object representing the primitive type Boolean.

The next segment is about the methods in the Boolean Class .

Methods 

booleanValue() : java.lang.Boolean.booleanValue() it assigns the value of a Boolean object to boolean primitive.

public class Example
{
public static void main(String[] args)
{
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("EDUREKA");
// getting primitive boolean value
boolean b4 = b1.booleanValue();
boolean b5 = b2.booleanValue();
boolean b6 = b3.booleanValue();
System.out.println(b4);
System.out.println(b5);
System.out.println(b6);
}
}

Output:
True
False
False
compareTo() : java.lang.Boolean.compareTo(Boolean arg) it compares this Boolean instance with the passed Boolean instance.
hashCode() : java.lang.Boolean.hashCode() it returns hash code value for the assigned boolean object.

public class Example
{
public static void main(String[] args)
{
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("TRue");
Boolean b4 = new Boolean(null);
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
System.out.println(b3.hashCode());
System.out.println(b4.hashCode());
}
}

Output:
1231
1237
1231
1237

toString() : java.lang.Boolean.toString() it returns string representation of the boolean object based on its value.

public class Example
{
public static void main(String[] args)
{
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("EDUREKA");
Boolean b4 = new Boolean(null);
// getting String value of Boolean objects
String str1 = b1.toString();
String str2 = b2.toString();
String str3 = b3.toString();
String str4 = b4.toString();

System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
}
}

Output:
True
False
False
False

Equals() : java.lang.Boolean.equals() it returns true iff you don’t pass a null argument. It should be a boolean object which represents the same boolean value as this object.

public class Example
{
public static void main(String[] args)
{
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("TrUe");
Boolean b4 = new Boolean(null);
// checking equality of Boolean objects
System.out.println(b1.equals(b2));
System.out.println(b2.equals(b4));
System.out.println(b1.equals(b3));
System.out.println(b1.equals(b4));
}
}

Output:
False
True
True
False

With this, we come to the end of this tutorial. I hope you are clear with this topic now. Keep reading, keep exploring!

If you found this article on “Boolean Class in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Boolean Class in Java” and our team will be glad to answer.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR A Comprehensive UI/UX Design Tutorial