简体   繁体   English

为什么 parseBoolean 不抛出异常?

[英]Why does parseBoolean not throw an exception?

Considering考虑到

boolean b = Boolean.parseBoolean("100");

I thought it would throw an exception because "100" is neither "true" nor "false" .我认为它会抛出异常,因为"100"既不是"true"也不是"false" But it passed, no exceptions.但它通过了,没有例外。

Why does the method not throw?为什么方法不抛出?

Reading the JavaDoc of the method (emphasis mine):阅读方法的JavaDoc (强调我的):

Parses the string argument as a boolean.将字符串参数解析为 boolean。 The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true" .如果字符串参数不是null并且忽略大小写等于字符串"true" ,则返回的 boolean 表示值true Otherwise, a false value is returned , including for a null argument.否则,将返回 false 值,包括 null 参数。

Looking at the implementation :查看实现

    public static boolean parseBoolean(String s) {
        return "true".equalsIgnoreCase(s);
    }

So anything not equal to the String "true" – ignoring case – is considered to be false所以任何不等于字符串"true"的东西——忽略大小写——都被认为是false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM