Java source code examples

Java source code samples. Java code examples.

Convert primitive boolean value to an object

          0 votes

We can use the Boolean wrapper class for converting a primitive boolean value to an object.

public class BooleanWrapper {
	public static void main(String args[]) {
		boolean bool = true;
		Boolean objBool = Boolean.valueOf(bool);

		System.out.println(objBool);
	}
}

or

public class BooleanWrapper {

	public static void main(String[] args) {
		boolean bool = true;
		Boolean objBool = new Boolean(bool);

		System.out.println(objBool);
	}
}

The first method would perform better. So it is the recommended way of creating a wrapper class for primitive boolean type.

Tags: java.lang

Discuss This Code