×

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

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Bottom view of a binary tree in Java

The lowest nodes in their horizontal distance are present and referred to as the bottom view of a binary tree. The horizontal distance between the nodes of a binary tree...

4 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

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.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

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

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.