为什么 Java 中的布尔值只接受 true 或 false?为什么不是 1 或 0 呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015071/
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
Why boolean in Java takes only true or false? Why not 1 or 0 also?
提问by GuruKulki
Is there any reason why Java booleans take only true
or false
why not 1
or 0
also?
有什么理由为什么 Java 布尔值只接受true
或false
为什么不接受1
或0
也接受?
采纳答案by JaredPar
Java, unlike languages like C and C++, treats boolean
as a completely separate data type which has 2 distinct values: true and false. The values 1 and 0 are of type int and are not implicitly convertible to boolean
.
与 C 和 C++ 之类的语言不同,Java 将其boolean
视为完全独立的数据类型,它具有 2 个不同的值:true 和 false。值 1 和 0 的类型为 int 并且不能隐式转换为boolean
。
回答by Andrzej Doyle
Because booleans have two values: true
or false
. Note that these are not strings, but actual boolean literals.
因为布尔值有两个值:true
or false
。请注意,这些不是字符串,而是实际的布尔文字。
1 and 0 are integers, and there is no reason to confuse things by making them "alternative true" and "alternative false" (or the other way round for those used to Unix exit codes?). With strong typing in Java there should only ever be exactly two primitive boolean values.
1 和 0 是整数,没有理由通过将它们设置为“替代真”和“替代假”(或者对于那些习惯于 Unix 退出代码的人来说相反?)来混淆事物。在 Java 中使用强类型应该只有两个原始布尔值。
EDIT: Note that you can easily write a conversion function if you want:
编辑:请注意,如果需要,您可以轻松编写转换函数:
public static boolean intToBool(int input)
{
if (input < 0 || input > 1)
{
throw new IllegalArgumentException("input must be 0 or 1");
}
// Note we designate 1 as true and 0 as false though some may disagree
return input == 1;
}
Though I wouldn't recommend this. Note how you cannot guarantee that an int
variable really is 0 or 1; and there's no 100% obvious semantics of what one means true. On the other hand, a boolean
variable is alwayseither true
or false
and it's obvious which one means true. :-)
虽然我不会推荐这个。请注意,您无法保证int
变量确实是 0 或 1;并且没有 100% 明显的语义是什么意思是真的。在另一方面,一个boolean
变量总是要么true
还是false
和很明显这一个手段如此。:-)
So instead of the conversion function, get used to using boolean
variables for everything that represents a true/false concept. If you must use some kind of primitive text string (e.g. for storing in a flat file), "true" and "false" are much clearer in their meaning, and can be immediately turned into a boolean by the library method Boolean.valueOf.
因此,不要使用转换函数,而是习惯于对boolean
代表真/假概念的所有事物使用变量。如果您必须使用某种原始文本字符串(例如用于存储在平面文件中),“true”和“false”的含义要清楚得多,并且可以通过库方法Boolean.valueOf立即转换为布尔值。
回答by Nathan Hughes
Because the people who created Java wanted boolean to mean unambiguously true or false, not 1 or 0.
因为创建 Java 的人希望 boolean 明确表示真或假,而不是 1 或 0。
There's no consensus among languages about how 1 and 0 convert to booleans. C uses any nonzero value to mean true and 0 to mean false, but some UNIX shells do the opposite. Using ints weakens type-checking, because the compiler can't guard against cases where the int value passed in isn't something that should be used in a boolean context.
关于如何将 1 和 0 转换为布尔值,语言之间没有达成共识。C 使用任何非零值表示真,使用 0 表示假,但某些 UNIX shell 则相反。使用 int 会削弱类型检查,因为编译器无法防止传入的 int 值不应该在布尔上下文中使用的情况。
回答by Shaun F
Being specific about this keeps you away from the whole TRUE in VB is -1 and in other langauges true is just NON ZERO. Keeping the boolean field as true or false keeps java outside of this argument.
具体到这一点让你远离整个 TRUE 在 VB 中是 -1,而在其他语言中,true 只是非零。将布尔字段保持为 true 或 false 会使 java 处于此参数之外。
回答by sateesh
On a related note: the java compiler uses intto represent booleansince JVM has a limited support for the boolean type.See Section 3.3.4 The boolean type.
相关说明:java 编译器使用int来表示布尔值,因为 JVM对布尔类型的支持有限。请参阅第 3.3.4 节布尔类型。
In JVM, the integer zero represents false, and any non-zero integer represents true(Source : Inside Java Virtual Machineby Bill Venners)
在 JVM 中,整数零代表false,任何非零整数代表true(来源:Inside Java Virtual Machineby Bill Venners)
回答by Stephen C
One thing that other answers haven't pointed out is that one advantage of not treating integers as truth values is that it avoids this C / C++ bug syndrome:
其他答案没有指出的一件事是,不将整数视为真值的一个优点是它避免了这种 C/C++ 错误综合症:
int i = 0;
if (i = 1) {
print("the sky is falling!\n");
}
In C / C++, the mistaken use of =
rather than ==
causes the condition to unexpectedly evaluate to "true" and update i
as an accidental side-effect.
在 C/C++ 中,错误地使用=
而不是==
导致条件意外评估为“真”并i
作为意外副作用更新。
In Java, that is a compilation error, because the value of the assigment i = 1
has type int
and a boolean
is required at that point. The only case where you'd get into trouble in Java is if you write lame code like this:
在 Java 中,这是一个编译错误,因为赋值的值i = 1
具有类型,int
并且boolean
此时需要a 。在 Java 中遇到麻烦的唯一情况是,如果您编写这样的蹩脚代码:
boolean ok = false;
if (ok = true) { // bug and lame style
print("the sky is falling!\n");
}
... which anyone with an ounce of "good taste" would write as ...
......任何有一点“好品味”的人都会写成......
boolean ok = false;
if (ok) {
print("the sky is falling!\n");
}
回答by pratikpchpr
In c and C++ there is no data type called BOOLEAN Thats why it uses 1 and 0 as true false value. and in JAVA 1 and 0 are count as an INTEGER type so it produces error in java. And java have its own boolean values true and false with boolean data type.
在 c 和 C++ 中,没有称为 BOOLEAN 的数据类型,这就是为什么它使用 1 和 0 作为真假值。并且在 JAVA 中 1 和 0 被视为整数类型,因此它会在 java 中产生错误。并且 java 有自己的布尔值 true 和 false 和布尔数据类型。
happy programming..
快乐编程..
回答by ElectricGeek
Even though there is a bool (short for boolean) data type in C++. But in C++, any nonzero value is a truevalue including negative numbers. A 0 (zero) is treated as false. Where as in JAVA there is a separate data type boolean for trueand false.
即使在 C++ 中有一个 bool(布尔的缩写)数据类型。但在 C++ 中,任何非零值都是真值,包括负数。0(零)被视为false。在 JAVA 中,true和false有一个单独的数据类型布尔值。