Java - Boolean booleanValue() method



Description

The Java Boolean booleanValue() returns the value of this Boolean object as a boolean primitive.

Declaration

Following is the declaration for java.lang.Boolean.booleanValue() method

public boolean booleanValue()

Parameters

NA

Return Value

This method returns the primitive boolean value of this object.

Exception

NA

Example 1

The following example shows the usage of Boolean booleanValue() method.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create a Boolean object b
      Boolean b;

      // assign value to b
      b = new Boolean(true);

      // create a boolean primitive type bool
      boolean bool;

      // assign primitive value of b to bool
      bool = b.booleanValue();

      String str = "Primitive value of Boolean object " + b + " is " + bool;

      // print bool value
      System.out.println( str );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Primitive value of Boolean object true is true

Example 2

The following is another example to show the usage of Boolean booleanValue() method.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create a Boolean object b
      Boolean b;

      // assign value to b
      b = Boolean.valueOf(false);

      // create a boolean primitive type bool
      boolean bool;

      // assign primitive value of b to bool
      bool = b.booleanValue();

      String str = "Primitive value of Boolean object " + b + " is " + bool;

      // print bool value
      System.out.println( str );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Primitive value of Boolean object false is false

Example 3

The following is another example to show the usage of Boolean booleanValue() method.

package com.tutorialspoint;
public class BooleanDemo {
   public static void main(String[] args) {

      // create a Boolean object b
      Boolean b;
      
      // assign value to b
      b = Boolean.valueOf(false);
      
      // create a boolean primitive type bool
      boolean bool;
      
      // assign a primitive value to Boolean object	  
      b = true;
      
      // assign primitive value of b to bool
      bool = b.booleanValue();
      
      String str = "Primitive value of Boolean object " + b + " is " + bool;
      
      // print bool value
      System.out.println( str );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Primitive value of Boolean object true is true
java_lang_boolean.htm
Advertisements