Converting a Java String Into a Boolean – 将一个Java字符串转换为布尔值

最后修改: 2020年 7月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll explore the different ways we can use Java’s Boolean class to convert a String into a boolean.

在本教程中,我们将探索使用Java的Boolean类来String转换为boolean的不同方法。

2. Boolean.parseBoolean()

2.Boolean.parseBoolean()

Boolean.parseBoolean() allows us to pass in a String and receive a primitive boolean.

Boolean.parseBoolean()允许我们传入一个String并接收一个原始的boolean

First, let’s write a test to see how parseBoolean() converts a String with the value true:

首先,让我们写一个测试,看看parseBoolean()如何转换一个字符串,其值为true:

assertThat(Boolean.parseBoolean("true")).isTrue();

Of course, the test passes.

当然,测试通过了。

In fact, the semantics of parseBoolean() are so clear that IntelliJ IDEA warns us that passing the string literal “true” is redundant.

事实上,parseBoolean()的语义是如此清晰,以至于IntelliJ IDEA警告我们,传递字符串字面“true”是多余的。

In other words, this method is excellent for turning a String into a boolean.

换句话说,这个方法很适合把字符串变成boolean

3. Boolean.valueOf()

3.Boolean.valueOf()

Boolean.valueOf() also lets us pass in a String, but this method returns a Boolean class instance instead of a primitive boolean.

Boolean.valueOf()也可以让我们传入一个String,但是这个方法返回一个Boolean类实例,而不是一个原始的boolean

We can see that this method also succeeds in converting our String:

我们可以看到,这个方法也成功地转换了我们的String:

assertThat(Boolean.valueOf("true")).isTrue();

This method actually uses parseBoolean() to do its String conversion in the background, and simply uses the result to return a statically defined Boolean instance.

这个方法实际上使用parseBoolean()在后台进行String转换,并简单地使用结果来返回一个静态定义的Boolean实例。

Therefore, this method should only be used if the returned Boolean instance is needed. If only a primitive result is needed, it’s more performant to stick with using parseBoolean() directly.

因此,只有在需要返回的Boolean实例时才应使用该方法。如果只需要一个原始的结果,坚持直接使用parseBoolean()会更有性能。

4. Boolean.getBoolean()

4.Boolean.getBoolean()

Boolean.getBoolean() is a third method that accepts a String and returns a boolean.

Boolean.getBoolean()是第三个方法,它接受一个String并返回一个boolean

Without looking at the documentation or the implementation of this method, one might reasonably assume that this method is also for converting its String argument into a boolean:

如果不看这个方法的文档或实现,我们可以合理地认为这个方法也是为了将其String参数转换为boolean:

assertThat(Boolean.getBoolean("true")).isTrue(); // this test fails!

The reason that this test fails is that the String argument is supposed to represent the name of a boolean system property. 

这个测试失败的原因是:String参数应该是代表boolean系统属性的名称。

By defining the system property:

通过定义系统属性。

System.setProperty("CODING_IS_FUN", "true");
assertThat(Boolean.getBoolean("CODING_IS_FUN")).isTrue();

Finally, the test passes. Inspecting the implementation of this method reveals that it, too, uses the parseBoolean() method to do its String conversion.

最后,测试通过了。检查这个方法的实现可以发现,它也使用parseBoolean()方法来进行String转换。

Note that getBoolean() is literally a shortcut for parseBoolean(System.getProperty(“true”)), meaning that we shouldn’t be misled by the name.

请注意,getBoolean()实际上是parseBoolean(System.getProperty(“true”))的快捷方式,这意味着我们不应该被这个名字误导。

Therefore, the only way Boolean.getBoolean(“true”); will ever return true is if there exists a system property called “true” and its value parses into true.

因此,Boolean.getBoolean(“true”);返回true的唯一方法是,存在一个名为“true”的系统属性,并且其值解析为true

4. Conclusion

4.总结

In this short tutorial, we have seen the key differences between Boolean.parseBoolean(), Boolean.valueOf(), and Boolean.getBoolean().

在这个简短的教程中,我们看到了Boolean.parseBoolean()Boolean.valueOf()Boolean.getBoolean()之间的关键区别。

While parseBoolean() and valueOf() both convert a String into a boolean, it’s important to remember that Boolean.getBoolean() does not.

虽然parseBoolean()valueOf()都将String转换为boolean,但必须记住Boolean.getBoolean()不会。

The source code with all the examples in this tutorial can be found over on Github.

本教程中包含所有示例的源代码可以在Github上找到over