Skip to Content

Convert Java boolean Primitive to Boolean object

  1. /*
  2.    Convert boolean to Boolean object Example
  3.    This example shows how a java boolean primitive can be converted
  4.    to a Boolean object.
  5. */
  6. public class BooleanPrimitiveToBooleanObjectExample {
  7.  
  8.   public static void main(String[] args) {
  9.     boolean b = true;
  10.    
  11.     //1. using constructor
  12.     Boolean blnObj1 = new Boolean(b);
  13.    
  14.     //2. using valueOf method of Boolean class. This is a static method.
  15.     Boolean blnObj2 = Boolean.valueOf(b);
  16.   }
  17. }
  18.  
  19. /*
  20. Output would be :
  21. true
  22. true
  23. */