Java Boolean.getBoolean("true") 返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21410683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:34:06  来源:igfitidea点击:

Boolean.getBoolean("true") returns false

java

提问by vico

I'm trying to make function that reads ini file. Why Boolean.getBoolean("true")returns false? How to use this conversation in correct way in java 1.4? Does it depends on system settings?

我正在尝试创建读取 ini 文件的函数。为什么 Boolean.getBoolean("true")返回false?如何在 java 1.4 中以正确的方式使用这个对话?这取决于系统设置吗?

采纳答案by Zach Thacker

Boolean.getBoolean()'s argument expects the name of a system property. What you're looking for is Boolean.valueOf("true")

Boolean.getBoolean()的参数需要系统属性的名称。你要找的是Boolean.valueOf("true")

回答by rhobincu

The method getBooleantakes a System Property name as an argument, not the String value of the boolean. What you need is probably Boolean.parseBoolean().

getBoolean方法将系统属性名称作为参数,而不是布尔值的字符串值。你需要的可能是Boolean.parseBoolean()

回答by Soccertrash

Boolean.getBoolean("true") has this javaDoc:

Boolean.getBoolean("true") 有这个 javaDoc:

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.

当且仅当由参数命名的系统属性存在且等于字符串“true”时,才返回 true。(从 JavaTM 平台的 1.0.2 版开始,此字符串的测试不区分大小写。)系统属性可通过 getProperty(由 System 类定义的方法)访问。如果没有指定名称的属性,或者指定名称为空或为空,则返回 false。

You are looking for Boolean.valueOf("true")

您正在寻找 Boolean.valueOf("true")

回答by Rakesh KR

From Boolean.getBoolean

来自Boolean.getBoolean

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.

当且仅当由参数命名的系统属性存在且等于字符串“true”时,才返回 true。

(从 JavaTM 平台的 1.0.2 版开始,此字符串的测试不区分大小写。)

系统属性可通过 getProperty(由 System 类定义的方法)访问。如果没有指定名称的属性,或者指定名称为空或为空,则返回 false。