望文生义不可取——Boolean.getBoolean()的陷阱

Boolean.getBoolean(String s)看起来是将字符串主换成boolean型,但实际上并非如此。

以下是API文档:

public static boolean getBoolean(String name)

Returns true if and only if the system property named by the argument exists and is equal to the string "true". (Beginning with version 1.0.2 of the JavaTM platform,the test of this string is case insensitive.) A system property is accessible through getProperty, a method defined by the System class.

If there is no property with the specified name, or if the specified name is empty or null, then false is returned.

Parameters: name - the system property name.

Returns: the boolean value of the system property.

该方法是查找系统参数中是否有与name同名的参数,有则返回true;无则返回false。并非将name转型为boolean。想要转型,可以使用:

Boolean.parseBoolean(String s)

Boolean.valueOf(String s).booleanValue()


-------------------------------------原创内容,转载请说明出处-----------------------------------------

欢迎联系

微信:oscaryue001


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值