[英]Convert boolean to int in Java
在 Java 中将boolean
转换为int
的最被接受的方法是什么?
int myInt = myBoolean ? 1 : 0;
^^ ^^
PS : true = 1 and false = 0 PS:真=1,假=0
int val = b? 1 : 0;
Using the ternary operator is the most simple, most efficient, and most readable way to do what you want.使用三元运算符是做您想做的事情的最简单、最有效和最易读的方法。 I encourage you to use this solution.
我鼓励您使用此解决方案。
However, I can't resist to propose an alternative, contrived, inefficient, unreadable solution.但是,我无法抗拒提出一个替代的、人为的、低效的、不可读的解决方案。
int boolToInt(Boolean b) {
return b.compareTo(false);
}
Hey, people like to vote for such cool answers !嘿,人们喜欢投票给这么酷的答案!
Edit编辑
By the way, I often saw conversions from a boolean to an int for the sole purpose of doing a comparison of the two values (generally, in implementations of compareTo
method).顺便说一句,我经常看到从布尔值到 int 的转换,其唯一目的是比较两个值(通常,在
compareTo
方法的实现中)。 Boolean#compareTo
is the way to go in those specific cases. Boolean#compareTo
是这些特定情况下的方法。
Edit 2编辑 2
Java 7 introduced a new utility function that works with primitive types directly, Boolean#compare
(Thanks shmosel ) Java 7 引入了一个新的实用函数,可以直接处理原始类型,
Boolean#compare
(感谢shmosel )
int boolToInt(boolean b) {
return Boolean.compare(b, false);
}
boolean b = ....;
int i = -("false".indexOf("" + b));
public int boolToInt(boolean b) {
return b ? 1 : 0;
}
simple简单的
import org.apache.commons.lang3.BooleanUtils;
boolean x = true;
int y= BooleanUtils.toInteger(x);
If you use Apache Commons Lang (which I think a lot of projects use it), you can just use it like this:如果您使用Apache Commons Lang (我认为很多项目都使用它),您可以像这样使用它:
int myInt = BooleanUtils.toInteger(boolean_expression);
toInteger
method returns 1 if boolean_expression
is true, 0 otherwise如果
boolean_expression
为真, toInteger
方法返回 1,否则返回 0
That depends on the situation.那要看情况了。 Often the most simple approach is the best because it is easy to understand:
通常最简单的方法是最好的,因为它很容易理解:
if (something) {
otherThing = 1;
} else {
otherThing = 0;
}
or或者
int otherThing = something ? 1 : 0;
But sometimes it useful to use an Enum instead of a boolean flag.但有时使用 Enum 而不是布尔标志很有用。 Let imagine there are synchronous and asynchronous processes:
假设有同步和异步进程:
Process process = Process.SYNCHRONOUS;
System.out.println(process.getCode());
In Java, enum can have additional attributes and methods:在 Java 中,枚举可以有额外的属性和方法:
public enum Process {
SYNCHRONOUS (0),
ASYNCHRONOUS (1);
private int code;
private Process (int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
If true -> 1
and false -> 0
mapping is what you want, you can do:如果
true -> 1
和false -> 0
映射是你想要的,你可以这样做:
boolean b = true;
int i = b ? 1 : 0; // assigns 1 to i.
If you want to obfuscate, use this:如果你想混淆,使用这个:
System.out.println( 1 & Boolean.hashCode( true ) >> 1 ); // 1
System.out.println( 1 & Boolean.hashCode( false ) >> 1 ); // 0
Lets play trick with Boolean.compare(boolean, boolean)
.让我们玩弄
Boolean.compare(boolean, boolean)
。 Default behavior of function: if both values are equal than it returns 0
otherwise -1
.函数的默认行为:如果两个值相等,则返回
0
否则返回-1
。
public int valueOf(Boolean flag) {
return Boolean.compare(flag, Boolean.TRUE) + 1;
}
Explanation : As we know default return of Boolean.compare is -1 in case of mis-match so +1 make return value to 0 for False
and 1 for True
说明:正如我们所知,在不匹配的情况下,Boolean.compare 的默认返回值为 -1,因此 +1 使返回值为0为
False
,为1为True
public int boolToInt(boolean b) {
return b ? 1 : 0;
}
public static int convBool(boolean b)
{
int convBool = 0;
if(b) convBool = 1;
return convBool;
}
Then use :然后使用:
convBool(aBool);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.