×

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

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Trim Method in String Java

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

3 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

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.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.