18 Aug Java Program to parse string to a Boolean object
To parse a string to a Boolean object, work with the valueOf() method in Java. The valueOf() returns a Boolean with a value represented by the specified string.
Syntax
public static Boolean valueOf(String str)
Let’s first create a Boolean:
Boolean b;
Now, use the valueOf() method to parse a string:
String str = "false"; Boolean b = Boolean.valueOf(str);
Let’s now see an example to parse string to a Boolean object and display the result:
Example
public class Example {
public static void main(String[] args) {
System.out.println("Parsing a string to a Boolean object...");
String str = "false";
System.out.println("String = "+str);
Boolean b = Boolean.valueOf(str);
System.out.println("Result = "+b);
}
}
Output
Parsing a string to a Boolean object... String = false Result = false
No Comments