1. Overview
1.概述
In this tutorial, we’ll learn how to convert a boolean value into an int value. First, we’ll look at how Java handles these two primitive data types; then, we’ll explore multiple ways to convert a boolean into an integer.
在本教程中,我们将学习如何将boolean值转换成int值。首先,我们将了解 Java 如何处理这两种原始数据类型;然后,我们将探讨将布尔值转换成整数的多种方法。
2. Data Types
2.数据类型
In Java, an integer can be represented by the int primitive data type or the Integer wrapper class. The primitive data type is a 32-bit signed integer represented by the Two’s Complement encoding method. The Integer class serves as a wrapper that allows you to perform unsigned integer operations, as well as to treat integer (primitive) values as objects to work with Generics.
在Java中,整数可以由int 原始数据类型或Integer 封装类来表示。原始数据类型是一个由Two’s Complement编码方法表示的32位有符号整数。整数类作为一个包装器,允许你进行无符号整数操作,以及将整数(原始)值作为对象来处理Generics。
On the other hand, boolean values don’t have a specific size in memory, but it defaults to the Operating System and Java Virtual Machine (JVM). Similarly, like all primitive data types in Java, boolean has the Boolean wrapper class that allows boolean values to behave like objects.
另一方面,boolean值在内存中没有特定的大小,但它默认为操作系统和Java虚拟机(JVM)。同样地,像Java中的所有原始数据类型一样,boolean也有布尔封装类,允许布尔值像对象一样行事。
We can leverage the primitive value and the wrapper class of both data types (boolean and int) to perform the data conversion. Assuming that the true and false boolean values represent 1 and 0, respectively, we have multiple approaches to make a conversion.
我们可以利用两种数据类型(boolean和int)的原始值和封装类来进行数据转换。假设true和falseboolean值分别代表1和0,我们有多种方法来进行转换。
3. Primitive boolean to int
3.原始的boolean到int
To convert a primitive boolean value into an int, we evaluate the condition of the expression to determine the integer we want to return:
要将一个原始的boolean 值转换成int。我们评估表达式的条件以确定我们要返回的整数。
public int booleanPrimitiveToInt(boolean foo) {
int bar = 0;
if (foo) {
bar = 1;
}
return bar;
}
We can simplify this function by using the ternary operator:
我们可以通过使用三元运算符来简化这个函数。
public int booleanPrimitiveToIntTernary(boolean foo) {
return (foo) ? 1 : 0;
}
This approach uses primitive data types (boolean and int) to make the conversion. As a result, we obtain 1 when the boolean expression is true. Otherwise, the method returns 0.
这种方法使用原始数据类型(boolean和int)来进行转换。结果是,当布尔表达式为真时,我们得到1。否则,该方法返回0。
4. Wrapper Class
4.包裹器类
Using the Boolean wrapper class, we have a couple of approaches to do the conversion:
使用布尔封装类,我们有几种方法来做转换。
- We can leverage the static methods from the Boolean class.
- We can call the methods directly from the Boolean object.
4.1. Static Methods
4.1.静态方法
The Boolean class has a compare method that we can use as follows:
布尔类有一个compare 方法,我们可以使用它,如下。
public static int booleanObjectToInt(boolean foo) {
return Boolean.compare(foo, false);
}
Recall that the static compare method returns 0 if both arguments have the same value. In other words, when foo is false, the comparison will result in 0. Otherwise, the function returns 1 when the first argument is true and the second argument is false.
记得静态compare方法返回0 如果两个参数有相同的值。 换句话说,当foo为假时,比较结果为0。否则,当第一个参数是true而第二个参数是false时,该函数返回1。
Similarly, we can use the same static method, changing the second argument to true:
类似地,我们可以使用同样的静态方法,将第二个参数改为true。
public static int booleanObjectToIntInverse(boolean foo) {
return Boolean.compare(foo, true) + 1;
}
This time, if foo is true, the compare method evaluates two arguments of the same value, which results in 0. However, adding 1 to the result will return the expected integer value from a truthy boolean variable.
这一次,如果foo为真,compare方法会评估两个相同值的参数,结果是0。然而,在结果中加入1将从一个真布尔变量中返回预期的整数值。
4.2. Boolean Class Object
4.2.布尔类对象
The Boolean class object has functions such as compareTo that we can use:
布尔类对象有诸如compareTo的函数,我们可以使用。
public static int booleanObjectMethodToInt(Boolean foo) {
return foo.compareTo(false);
}
Using the method booleanObjectMethodToInt, we can convert a boolean value to an integer the same way we did with the static method. Similarly, you can apply the reversed version by changing the argument to true and adding 1 to the result.
使用方法booleanObjectMethodToInt,我们可以像使用静态方法那样将布尔值转换成整数。同样地,你可以通过将参数改为true并在结果中加入1来应用反转版本。
5. Apache Commons
5.阿帕奇公社
Apache Commons is a popular open-source library for Java that provides utility class, such as BooleanUtils. We can add the library as a dependency in Maven as follows:
Apache Commons是一个流行的Java开源库,提供实用类,如BooleanUtils。我们可以在Maven中把该库作为依赖项添加,方法如下。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Once the library is in our pom.xml file, we can use the BooleanUtils class to convert boolean values to integer:
一旦库在我们的pom.xml文件中,我们可以使用BooleanUtils类将布尔值转换为整数。
public static int booleanUtilsToInt(Boolean foo) {
return BooleanUtils.toInteger(foo);
}
Like the example method booleanPrimitiveToIntTernary, internally, the toInteger method performs the same ternary operator to make the conversion.
像示例方法booleanPrimitiveToIntTernary一样,在内部,toInteger方法执行相同的三元运算符来进行转换。
6. Conclusion
6.结语
In this tutorial, we’ve learned how to convert a boolean into an integer value. Assuming that true translates to 1 and false translates to 0, we explored different implementations to achieve this conversion.
在本教程中,我们已经学会了如何将布尔值转换为整数值。假设true转换为1,false转换为0,我们探索了不同的实现方式来实现这种转换。
As always, the complete code samples for this article can be found over on GitHub.
一如既往,本文的完整代码样本可以在GitHub上找到。