Convert String to boolean primitive in Java: parseBoolean() method

How to convert String object to boolean primitive in Java.

Example: Complete conversion code
In this example we are converting string to boolean using parseBoolean() method of Boolean class.

class StringToboolean {
   public static void main(String[] args) {
      // String Objects
      String str = "false";
      // Case does not matter for conversion
      String str2 = "TrUe";
      // String to boolean conversion
      boolean bvar = Boolean.parseBoolean(str);
      boolean bvar2 = Boolean.parseBoolean(str2);
      // Displaying boolean values
      System.out.println("boolean value of String str is: "+bvar);
      System.out.println("boolean value of String str2 is: "+bvar2);

boolean value of String str is: false
boolean value of String str2 is: true
public static boolean parseBoolean(String s): Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string ‚Äútrue‚ÄĚ.
Example: Boolean.parseBoolean(‚ÄúTrue‚ÄĚ) returns true.
Example: Boolean.parseBoolean(‚Äúyes‚ÄĚ) returns false.

asked Jun 11, 2015 in Java Interview Questions by rajesh
0 votes

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.