×

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

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

&& Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

Pyramid Program in Java

Pyramid Program in Java In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except...

2 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.