Java source code examples

Java source code samples. Java code examples.

Convert a String to boolean object.

          0 votes

We can use the Boolean wrapper class for converting a String object to boolean object.

public class BooleanWrapper {
	public static void main(String[] args) {
		String str = "true";
		Boolean objBool = new Boolean(str);

		System.out.println(objBool);
	}
}

or

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

		System.out.println(objBool);
	}
}

We can convert any String type to a Boolean object using the above code. Any value other than 'true' for the String would yield a Boolean object with value false.

Tags: java.lang

Discuss This Code