javastringboolean

How to check if String value is Boolean type in Java?


I did a little search on this but couldn't find anything useful.

The point being that if String value is either "true" or "false" the return value should be true. In every other value it should be false.

I tried these:

String value = "false";
System.out.println("test1: " + Boolean.parseBoolean(value));
System.out.println("test2: " + Boolean.valueOf(value));
System.out.println("test3: " + Boolean.getBoolean(value));

All functions returned false :(


Solution

  • There exists no method to test whether a String encodes a Boolean; for all practical effects, any input other than true (ignoring case) returns false.

    Method Returns
    Boolean.parseBoolean( string ) primitive boolean
    Boolean.valueOf( string ) object Boolean