Description
Program to convert boolean primitive to Boolean object
Example
class BooleanPrimToBooleanObj { public static void main(String[] args) { boolean bvar = true; /* Method 1: By passing boolean value to * the constructor of Boolean class */ Boolean bObj = new Boolean(bvar); System.out.println(bObj); /* Method 2: By passing boolean value to the * valueOf() method of Boolean class. */ Boolean bObj2 = Boolean.valueOf(bvar); System.out.println(bObj2); } }
Output:
true true
public static Boolean valueOf(boolean b)
: Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time performance.
Leave a Reply