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
1 2 3 |
public static Boolean valueOf(String str) |
Let’s first create a Boolean:
1 2 3 |
Boolean b; |
Now, use the valueOf() method to parse a string:
1 2 3 4 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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
1 2 3 4 5 |
Parsing a string to a Boolean object... String = false Result = false |
No Comments