parseBoolean是包裝類下Boolean下的方法,爲了更明白一些,我們來看一下源代碼
/**
* Parses the string argument as a boolean. The <code>boolean</code>
* returned represents the value <code>true</code> if the string argument
* is not <code>null</code> and is equal, ignoring case, to the string
* {@code "true"}. <p>
* Example: {@code Boolean.parseBoolean("True")} returns <tt>true</tt>.<br>
* Example: {@code Boolean.parseBoolean("yes")} returns <tt>false</tt>.
*
* @param s the <code>String</code> containing the boolean
* representation to be parsed
* @return the boolean represented by the string argument
* @since 1.5
*/
public static boolean parseBoolean(String s) {
return toBoolean(s);
}
所以:
public class test {
public static void main(String[] args){
String s = "true";
boolean a=Boolean.parseBoolean(s);
System.out.println(a);
}
}
結果爲:true
而當s爲其他的值時,結果爲:false
應該常看java的源代碼,這樣能理解最深層的原因。