Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法

正确用法: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
}

}

原文地址:https://www.cnblogs.com/viewcozy/p/4892736.html