Parse a string to a Boolean object in Java


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.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements