Java Boolean parseBoolean() Method
The parseBoolean() method of Boolean class returns a Boolean value for the specified String argument. It returns true if and only if string’s value is equal to “true”, else it returns false.
Syntax:
public static boolean parseBoolean(String s)
Parameters:
The parameter ‘s’ represents the string value.
Return Value:
The parseBoolean() method returns the Boolean value represented by the string argument ‘s’.
- It returns true, if and only if the string argument is equal (ignoring case) to the string “true”.
- It returns false for any other argument other than true.
Example 1:
public class JavaBooleanparseBooleanMethodExample1 {
static int i=1;
public static void main(String[] args) {
String str="true";
//returns the Boolean value represented by the string argument
Boolean val1=(Boolean.parseBoolean(str));
System.out.println(i+ ". "+val1);
}
}
Output:
1. true
Example 2:
public class JavaBooleanParseBooleanMethodExample2 {
static int i=1;
public static void main(String[] args) {
String str=null;
//returns the boolean value false for null
Boolean val1=(Boolean.parseBoolean(str));
System.out.println(i+ ". "+val1);
}
}
Output:
1. false
Example 3:
public class JavaBooleanParseBooleanMethodExample3 {
static int i=1;
public static void main(String[] args) {
String str="aanmsggsavbj";
//returns the boolean value false for any other string argument
Boolean val1=(Boolean.parseBoolean(str));
System.out.println(i+ ". "+val1);
}
}
Output:
1. false
