正確用法:boolean repeatIndicator = Boolean.valueOf("true").booleanValue();
或者也可以使用Boolean.parseBoolean()方法,但此方法是jdk1.5以后推出的。
以下是Boolean.getBoolean的正確用法:
public class TestGetBoolean
{
public static void main(String[] args){
//大寫的true返回為false,必須是小寫的true
String s1 = "true";
String s2 = new String("true");
//這里將s1存放到Java系統屬性中了.
System.setProperty(s1,"true");
System.setProperty(s2,"true");
//這里從系統屬性中獲取s1,所以獲取到了。
System.out.println(Boolean.getBoolean(s1));//true
System.out.println(Boolean.getBoolean(s2));//true
String s3 = "true";
//很明顯s3並沒有存放到系統屬性中所以返回false。
System.out.println(Boolean.getBoolean(s3));//false
//這個更是錯的了,呵呵。
System.out.println(Boolean.getBoolean("true"));//false
}
}