public class ChangeBoolean {
public static void main(String args[]){
Boolean b = new Boolean("ok");
String str = b.toString();
System.out.println("ok:" + str);
b = new Boolean("true");
str = b.toString();
System.out.println("true:" + str);
}
}
运行结果:
ok:false
true:true
只有 true 和 1 等可以被识别为true,否则就是false
Boolean
public Boolean(String s)
Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, allocate a Boolean object representing the value false. Examples:
new Boolean("True") produces a Boolean object that represents true.
new Boolean("yes") produces a Boolean object that represents false.
new Boolean()对象时由于Boolean类提供了构造参数的写法,如果传的参数是字符串则“true”时,返回true,其它任何是否都返回false;
这是jdk提供的源码
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
看完API应该就明白了,希望我的回答对你有帮助。