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

Getting Primitive boolean from Boolean Object of true value Example

The following example shows the usage of Boolean booleanValue() method. In this program, we've created a Boolean variable and assigned it a Boolean Object of true value. Then using booleanValue(), the underlying value of Boolean variable is assigned to primitive boolean variable and result is printed.

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

Getting Primitive boolean from Boolean Object of false value Example

The following example shows the usage of Boolean booleanValue() method. In this program, we've created a Boolean variable and assigned it a Boolean Object of false value. Then using booleanValue(), the underlying value of Boolean variable is assigned to primitive boolean variable and result is printed.

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

Getting Primitive boolean from Boolean Object of false value Example

The following example shows the usage of Boolean booleanValue() method. In this program, we've created a Boolean variable and assigned it a Boolean Object of false value. Then using booleanValue(), the underlying value of Boolean variable is assigned to primitive boolean variable and result is printed.

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