这是一个比较古怪的知识点,本身算不了什么。可能是这个构造器太过古怪,从 Java 9 开始,这个构造器已经标记为 @Deprecated 了,相应地,可以使用 Boolean.parseBoolean(String s) 来实现此功能,两个方法都支持 null 参数。
Boolean b = new Boolean("abcd");
上面这句代码看起来好像不正常,但其实是「正常」的。不过,它执行后,b 的值到是 false 还是 true?
查 API 文档,原来 Boobean 确实有这么个构造器,而且,当字串为 “true”(大小写无关)时,值为 true,其余情况,皆为 false. 具体的 API 资料抄录如下:
public Boolean(String s)
Allocates a
Booleanobject representing the valuetrueif the string argument is notnulland is equal, ignoring >case, to the string “true”. Otherwise, allocate aBooleanobject representing the valuefalse. Examples:
new Boolean("True")produces a Boolean object that representstrue.
new Boolean("yes")produces a Boolean object that representsfalse.Parameters:
s - the string to be converted to a
Boolean.
这个构造器直到 Java 21 还在,参这里:
Boolean(String s)
Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.