×

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument.

Syntax

public Boolean booleanValue()

Parameters

NA

Return Value

This method returns the primitive value of specified Boolean object.

Example 1

public class JavaBooleanBooleanValueExample1 {
    static int i=1;
    public static void main(String[] args) {
        // create a boolean object
        Boolean b1 = new Boolean(true);
        //returns a Boolean value for the specified Boolean arguments
        Boolean b2= b1.booleanValue();
        System.out.println(i + ". Boolean value of "+b1 +" : "+b2);
        Boolean b3 = false;
        //assigning boolean value of b3 to b4
        boolean b4 = b3.booleanValue();
        System.out.println(i++ +". Boolean value for "+b3+" : "+b4);
    }
}

Output

1. Boolean value of true : true
2. Boolean value for false : false

Example2:

import java.util.Scanner;
public class JavaBooleanBooleanValueExample2 {
     public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
         System.out.println("Enter your name and age");
         System.out.print("Name1 : ");String str1 =scanner.next();
         System.out.print("Age1 :"); int age1=scanner.nextInt();
         System.out.println();
         System.out.print("Name2 : ");String str2 =scanner.next();
         System.out.print("Age2 :"); int age2=scanner.nextInt();
         Boolean b3=(age1>age2)?true:false;
           if(b3.booleanValue()){
                System.out.println(str1+" is elder than "+str2);
            }
           else{
               System.out.println(str2+" is elder than "+str1);
            }
        }
    } 

Output:

Enter your name and age
Name1 : Reema
Age1 :21
Name2 : Geetanjali
Age2 :22
Geetanjali is elder than Reema

Example 3

public class JavaBooleanBooleanValueExample3 {
    public static void main(String[] args) {
        //for null value it will give an exception
        Boolean b1 = null;
        Boolean b2=b1.booleanValue();
        System.out.println("Value is "+b2);
    }
}

Output

Exception in thread "main" java.lang.NullPointerException
      at com.TutorialAndExample.JavaBooleanBooleanValueExample3.main(JavaBooleanBooleanValue
Example3.java:7)

Related Topics

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java While Keyword

Depending on a specified Boolean condition, a while loop in Java allows code to be executed repeatedly. The while loop can be viewed as an iterative version of the if...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

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

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.