Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Creation of Boolean Class Objects
3.
Fields
4.
Methods
4.1.
static boolean parseBoolean(String s)
4.2.
boolean booleanValue()
4.3.
static Boolean valueOf(boolean b)
4.4.
static Boolean valueOf(String s)
4.5.
static String toString(boolean b)
4.6.
String toString()
4.7.
int hashCode()
4.8.
boolean equals(Object obj)
4.9.
int compareTo(Boolean b)
4.10.
int compare(boolean x, boolean y)
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Boolean Class in Java

Author Pankhuri Goel
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

Recommended Topic, Multithreading in javaDuck Number 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);
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Fields

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.
     

Also see,  Swap Function in Java

Methods

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

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.

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.

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.

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.

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.

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.

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.

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.

Syntax: 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.

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.

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.

Must Read Static Blocks In Java and Hashcode Method in Java

FAQs

  1. What is a wrapper class?
    A wrapper class is a type that wraps or contains primitive data types in its object. When we construct an object for a wrapper class, it includes a field where we can store primitive data types. In other words, a primitive value can be wrapped into a wrapper class object.  
     
  2. Describe the keyword ‘static.’
    In Java, the static keyword is primarily used to control memory. In Java, the static keyword is used to share a class’s identical variable or method. Static keywords can be used with variables, methods, blocks, and nested classes. The static keyword belongs to a class rather than a specific class instance. A constant variable or a method that is the same for every class instance is designated with the static keyword. 
     
  3. Define data types. Name the primary data types in Java.
    The different sizes and values that can be stored in the variable are defined by data types. In Java, there are two primary types of data types:
    → Primitive data consists solely of single values with no additional capabilities. Boolean, int, char, byte, float, double, short, and long are primitive data types.
    → The Non-Primitive data types will contain a memory address because variable values will not be stored directly in memory. They include classes, objects, arrays, etc.

Key Takeaways

In this article, we learned about the Boolean class in Java. We also saw various methods available in the class, like parseBoolean, hashCode and compare.

We hope this blog has helped you enhance your knowledge. If you want to learn more, check out our articles on Introduction To Java - Coding Ninjas Coding Ninjas StudioIntroduction to Data Type - Coding Ninjas Coding Ninjas Studio and Wrapper class in Java - Coding Ninjas Coding Ninjas Studio.Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more!

Happy Reading!

Previous article
Java Double Class
Next article
Autoboxing and Unboxing
Live masterclass