java boolean转string,关于Java的Boolean.getBoolean方法和String转换Boolean的问题

本文探讨了在Java中使用Boolean.getBoolean方法判断系统属性值的问题,指出其行为并非直接将字符串转化为布尔值,而是依赖于system.getProperty方法。作者通过深入理解system.getProperty的特性,揭示了如何正确处理字符串到Boolean的转换,并提醒开发者注意API细节以避免误解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天读配置文件里面有Boolean类型的属性,之前都是 使用下面代码判断:

Java

value.equals("true")

1

value.equals("true")

感觉有点不优雅,于是用了一个Boolean类的静态方法Boolean.getBoolean,结果打断点一看:

Java

Boolean.getBoolean("true"); // false

1

Boolean.getBoolean("true");// false

返回false,有点莫名其妙。

断电进去看了一下getBoolean的源码:

Java

/**

* Returns {@code true} if and only if the system property

* named by the argument exists and is equal to the string

* {@code "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 {@code getProperty}, a method defined by the

* {@code System} class.

*

* If there is no property with the specified name, or if the specified

* name is empty or null, then {@code false} is returned.

*

* @param name the system property name.

* @return the {@code boolean} value of the system property.

* @see java.lang.System#getProperty(java.lang.String)

* @see java.lang.System#getProperty(java.lang.String, java.lang.String)

*/

public static boolean getBoolean(String name) {

boolean result = false;

try {

result = toBoolean(System.getProperty(name));

} catch (IllegalArgumentException e) {

} catch (NullPointerException e) {

}

return result;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

/**

* Returns {@code true} if and only if the system property

* named by the argument exists and is equal to the string

* {@code "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 {@code getProperty}, a method defined by the

* {@code System} class.

*

* If there is no property with the specified name, or if the specified

* name is empty or null, then {@code false} is returned.

*

* @param   name   the system property name.

* @return  the {@code boolean} value of the system property.

* @see     java.lang.System#getProperty(java.lang.String)

* @see     java.lang.System#getProperty(java.lang.String, java.lang.String)

*/

publicstaticbooleangetBoolean(Stringname){

booleanresult=false;

try{

result=toBoolean(System.getProperty(name));

}catch(IllegalArgumentExceptione){

}catch(NullPointerExceptione){

}

returnresult;

}

呃,看到有个  System.getProperty(name)  感觉不对劲

看一下注释原来这个方法

当且仅当以参数命名的系统属性存在,且等于 “true” 字符串时,才返回 true。(从 JavaTM 1.0.2 平台开始,字符串的测试不再区分大

小写。)通过 getProperty 方法可访问系统属性,此方法由 System 类定义。

如果没有以指定名称命名的属性或者指定名称为空或 null,则返回 false。

System.getProperty都有哪些呢?跟一下断点:

Java

/**

* System properties. The following properties are guaranteed to be defined:

*

*

java.version Java version number

*

java.vendor Java vendor specific string

*

java.vendor.url Java vendor URL

*

java.home Java installation directory

*

java.class.version Java class version number

*

java.class.path Java classpath

*

os.name Operating System Name

*

os.arch Operating System Architecture

*

os.version Operating System Version

*

file.separator File separator ("/" on Unix)

*

path.separator Path separator (":" on Unix)

*

line.separator Line separator ("\n" on Unix)

*

user.name User account name

*

user.home User home directory

*

user.dir User's current working directory

*

*/

private static Properties props;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

* System properties. The following properties are guaranteed to be defined:

*

*

java.version         Java version number

*

java.vendor          Java vendor specific string

*

java.vendor.url      Java vendor URL

*

java.home            Java installation directory

*

java.class.version   Java class version number

*

java.class.path      Java classpath

*

os.name              Operating System Name

*

os.arch              Operating System Architecture

*

os.version           Operating System Version

*

file.separator       File separator ("/" on Unix)

*

path.separator       Path separator (":" on Unix)

*

line.separator       Line separator ("\n" on Unix)

*

user.name            User account name

*

user.home            User home directory

*

user.dir             User's current working directory

*

*/

privatestaticPropertiesprops;

只有发的参数是上面的字符串并且该属性的值为true时 才会返回  true

呃,那么怎么才能把字符串转成Boolean呢?  继续看Boolean源码,发现私有方法:

Java

private static boolean toBoolean(String name) {

return ((name != null) && name.equalsIgnoreCase("true"));

}

1

2

3

privatestaticbooleantoBoolean(Stringname){

return((name!=null)&&name.equalsIgnoreCase("true"));

}

那么都有哪些方法调用它呢?

构造方法:

Java

public Boolean(String s) {

this(toBoolean(s));

}

1

2

3

publicBoolean(Strings){

this(toBoolean(s));

}

其他方法:

Java

public static boolean parseBoolean(String s) {

return toBoolean(s);

}

1

2

3

publicstaticbooleanparseBoolean(Strings){

returntoBoolean(s);

}

Java

public static Boolean valueOf(String s) {

return toBoolean(s) ? TRUE : FALSE;

}

1

2

3

publicstaticBooleanvalueOf(Strings){

returntoBoolean(s)?TRUE:FALSE;

}

所以类型转换还是使用paese开头的方法才是正确的姿态。不看API容易进坑啊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值