Convert Java String Object to Boolean Object

Java 8Object Oriented ProgrammingProgramming

A string object can be created in Java using the string literal.

String myStr = “Amit”;

Another way to create a string object is using the new keyword.

String myStr = new String("Hello!");

We have used the first method to create a string object.

String str = "true";

Now, use the valueOf() method to convert String Object to Boolean Object. We have used this method on Boolean object.

Boolean bool = Boolean.valueOf(str);

Let us now see the complete example to show how to convert String Object to Boolean Object.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String str = "true";
      // conversion
      Boolean bool = Boolean.valueOf(str);
      System.out.println(bool);
   }
}

Output

True
raja
Published on 13-Nov-2018 09:56:58
Advertisements