Java source code examples

Java source code samples. Java code examples.

Convert a String to primitive boolean

          0 votes

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

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

		System.out.println(bool);
	}
}

or

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

		System.out.println(b);
	}
}

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

Tags: java.lang

Discuss This Code