×

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

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

AbstractSet Class in Java

The AbstractSet class is used for the implementation of the Abstract Collection class and interface. It is the part of Collection Frameworks. In the AbstractSet, the implementation is same as the...

3 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Print Matrix Diagonally in Java

The aim is to print the elements of a matrix of size n*n in some kind of a diagonal pattern. Input : mat[3][3] = {{1, 2, 3},                      {4, 5, 6},                      {7,...

3 minutes read.

Hierarchy of operators in Java

Operators are the most frequently used terminology in any area of programming, and it helps in various approaches to efficiently solve daily life problems by computer programming. The simple addition of...

4 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.