OneBite.Dev - Coding blog in a bite size

convert variable from string to boolean in java

Code snippet on how to convert variable from string to boolean in java

  boolean variableName = Boolean.parseBoolean(stringVariableName);

This piece of code converts a variable from a string to a boolean in Java. The Boolean class has a static parseBoolean method which takes in a String and returns a boolean value. The boolean variableName created is set to the result of parsing the string variable with the parseBoolean method. This returns true if the String is equal to the String literal “true” and false otherwise. Therefore this code converts a String variable to a boolean variable, assuming the value in the String variable is either “true” or “false”.

java