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)