Parse a string to a Boolean object in Java

Java 8Object Oriented ProgrammingProgramming

The valueOf() method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, Boolean, etc. Therefore, to parse a string to a Boolean object, use the Java valueOf() method.

The following is an example showing how to parse a string to a Boolean object in Java.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      System.out.println("Parsing a string to a Boolean object...");
      Boolean val = Boolean.valueOf("true");
      System.out.println("Value: "+val);
   }
}

Output

Parsing a string to a Boolean object...
Value: true

Boolean object is used to parse a string in the above program.

Boolean val = Boolean.valueOf("true");

We have passed a string “true” to valueOf() method above. This string is parsed.

raja
Published on 12-Nov-2018 14:51:37
Advertisements