×

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean.

Boolean Methods:

This class contains several different methods and other constants for converting Boolean to a string and vice versa and also many other methods which are useful dealing with a Boolean.

MethodsDescription
booleanValue()It returns a Boolean value for the Boolean arguments.
compare()It compares the two Boolean arguments.
compareTo()It compares the given Boolean instance with another.
equals()It returns a Boolean value true only if the specified argument is not null and the Boolean argument represents the same value as the object.
getBoolean()It returns a Boolean value true if and only if the system property name is not null and is equal to ”true”.
hashCode()It returns a hash code for the specified Boolean object.
logicalAnd()It returns the result after implementing logical AND operation on the Boolean arguments.
logicalOr()It returns the result after implementing logical OR operation on the specified Boolean arguments.
logicalXor()It returns the result after implementing logical XOR operation on the Boolean arguments.
parseBoolean()It returns a Boolean value represented by the string argument by parsing the string argument as a Boolean.
toString()It returns a String instance representing the specified Boolean’s value.
valueOf()It returns a Boolean instance representing the given string or Boolean value.

Example 1:

public class JavaBooleanClassExample1 {
    static  int i=1;
    public static void main(String[] args) {
        Boolean b1= true;
        Boolean b2=true;
        //assigning boolean value of b1 to b3
        Boolean b3= b1.booleanValue();
            System.out.println(i++ + ". booleanValue method will  return : "+b3);
        //check whether b1 and b2 are equal or not
        Boolean b4 = b1.equals(b2);
            System.out.println(i++ + ". equals method will return : "+b4);
        // logicalOr() with return the same result as OR operator
        Boolean b5 = Boolean.logicalOr(b1,b2);
            System.out.println(i++ + ". Logical And will return: "+b5);
    }
}

Output:

1. booleanValue method will return : true
2. equals method will return : true
3. Logical And will return: true

Example 2:

public class JavaBooleanClassExample2 {
    static  int i=1;
    public static void main(String[] args) {
        Boolean b1= true;
        Boolean b2=true;
        //return the hash code for the boolean value
        int b3= b1.hashCode();
            System.out.println(i++ + ". Hash code of"+ b1+"  : "+b3);
        //parses the string argument to boolean
        String str = "false";
        Boolean b4= Boolean.parseBoolean(str);
            System.out.println(i++ + ". parseBoolean method will return : "+b4);
        // returns a String instance representing the specified Boolean’s value
        String b5 = Boolean.toString(b1);
            System.out.println(i++ + ". toString will return : "+b5);
        // returns a Boolean instance representing the specified boolean value.
        Boolean b6 = Boolean.valueOf(b1);
        System.out.println(i++ + ". valueOf method will return : "+b5);
    }
}

Output:

1. Hash code oftrue : 1231
2. parseBoolean method will return : false
3. toString will return : true
4. valueOf method will return : true

Related Topics

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Compile-time Error in Java

In java, the execution of a program is stopped due to the occurrence of some problem known as an error. Errors are illegal operations that are carried out by the...

4 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Java FileOutputStream

What is FileOutputStream?When we need raw stream data written into a file, we need to look for another option: FileOutputStream. It is used when the file's data is byte-oriented. It comes under...

4 minutes read.