×

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

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 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.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 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.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.