» » Boolean wrapper class example programs

  • java.lang.Boolean Class is used to represent primitive boolean value in the form of object.

  1.  public final class Boolean
  2.    extends Object
  3.       implements Serializable, Comparable<Boolean>
     

Boolean Class Constructors:

1. public Boolean(boolean value)

  1. Boolean obj=new Boolean(true);

Java code to convert boolean primitive to Boolean object


  1. package com.instanceofjavatutorial;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  


  7.  Boolean obj=new Boolean(false);
  8.  
  9.  System.out.println(obj);
  10.  
  11.  
  12. }
  13. }

Output:
  1. true

2. public Boolean(String s)

  1. Boolean b=new Boolean("true");

Java Program to Convert String Object to Boolean Object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="true";

  8.  Boolean obj=new Boolean(str);
  9.  
  10.  System.out.println(obj);
  11.  
  12.  
  13. }
  14. }

Output:
  1. true

Boolean class Methods:

1.public boolean booleanValue()

  • This method gives the boolean primitive value from Boolean object
  • See below java code which explains how to get boolean primitive value and assig to primitive variable from Boolean Object.

Java Program To Convert boolean primitive value to Boolean Object and To get boolean value from Boolean Object

  1. package com.instanceofjavatutorial;
  2.  
  3. public class BooleanDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. // converting boolean variable in to Boolean Object
  8.  Boolean bobj=new Boolean(true);
  9.  
  10.  System.out.println(bobj);
  11.  
  12. //converting Bolean object into boolean primitive value
  13.  boolean bval=bobj.booleanValue();
  14.  
  15.  System.out.println(bval);
  16.  
  17. }
  18. }

Output:
  1. true
  2. true

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu