在 Java 中将布尔值转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3793650/
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
Convert boolean to int in Java
提问by hpique
What is the most accepted way to convert a boolean
to an int
in Java?
在 Java 中将a 转换boolean
为 an的最被接受的方法是什么int
?
采纳答案by Jonatha ANTOINE
int myInt = myBoolean ? 1 : 0;
^^
^^
PS : true = 1 and false = 0
PS:真=1,假=0
回答by codaddict
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.
回答by Grodriguez
int val = b? 1 : 0;
回答by Thorbj?rn Ravn Andersen
boolean b = ....;
int i = -("false".indexOf("" + b));
回答by Hendrik Brummermann
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. Let imagine there are synchronous and asynchronous processes:
但有时使用 Enum 而不是布尔标志很有用。假设有同步和异步进程:
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;
}
}
回答by Adam
public int boolToInt(boolean b) {
return b ? 1 : 0;
}
simple
简单的
回答by barjak
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). Boolean#compareTo
is the way to go in those specific cases.
顺便说一下,我经常看到从 boolean 到 int 的转换,其唯一目的是比较两个值(通常是在compareTo
方法的实现中)。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);
}
回答by mumair
Lets play trick with Boolean.compare(boolean, boolean)
. Default behavior of function: if both values are equal than it returns 0
otherwise -1
.
让我们玩把戏Boolean.compare(boolean, boolean)
。函数的默认行为:如果两个值相等则返回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 0for False
and 1for True
说明:正如我们所知,在不匹配的情况下,Boolean.compare 的默认返回值为 -1,因此 +1 使返回值为0forFalse
和1forTrue
回答by Darkeniel
import org.apache.commons.lang3.BooleanUtils;
boolean x = true;
int y= BooleanUtils.toInteger(x);
回答by Christian Ullenboom
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