Java Boolean.parseBoolean() Method: Convert String to Boolean
The Boolean.parseBoolean() method in Java is used to convert a string representation of a boolean value into its corresponding boolean primitive type. It returns 'true' if the string is 'true' (case insensitive), and 'false' otherwise.
Syntax:
Boolean.parseBoolean(String string)
Example:
String str1 = "true";
String str2 = "false";
String str3 = "TRUE";
String str4 = "False";
String str5 = "abc";
boolean bool1 = Boolean.parseBoolean(str1); // true
boolean bool2 = Boolean.parseBoolean(str2); // false
boolean bool3 = Boolean.parseBoolean(str3); // true
boolean bool4 = Boolean.parseBoolean(str4); // false
boolean bool5 = Boolean.parseBoolean(str5); // false
Explanation:
- The
parseBoolean()method accepts a single argument, aStringobject. - It returns a
booleanprimitive value. - If the string is equal to 'true' (case-insensitive), it returns
true. - If the string is equal to 'false' (case-insensitive), it returns
false. - For any other input string, it returns
false.
Note: The parseBoolean() method is a static method, meaning you can call it directly on the Boolean class without creating an instance of it.
原文地址: https://www.cveoy.top/t/topic/qqTt 著作权归作者所有。请勿转载和采集!