为什么 Java 不允许转换 Boolean -> Int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16281760/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:30:36  来源:igfitidea点击:

Why Doesn't Java Allow Casting Boolean -> Int?

javatypescasting

提问by Jared Nielsen

I was wondering why Java doesn't allow casting from a boolean to an int, like so:

我想知道为什么 Java 不允许从 boolean 转换为 int,如下所示:

boolean foo = true;
int bar = (int)foo;

This can be done in one line of code, for example,

这可以在一行代码中完成,例如,

bar = foo ? 1 : 0;

but it seems like an better and easier-to-read way would be to allow type-casting, as with doubleand int. Why doesn't Java include this feature?

但似乎更好且更易于阅读的方法是允许类型转换,如doubleint。为什么 Java 不包含此功能?

回答by Stephen C

It doesn't allow this because the Java designers (correctly) recognized that the boolean / integer overloading in C and C++ was a significant source of errors.

它不允许这样做,因为 Java 设计者(正确地)认识到 C 和 C++ 中的布尔值/整数重载是错误的重要来源。

(I recall seeing that in writing in some design rationale, but I can't find it.)

(我记得在一些设计原理中看到过,但我找不到。)

For instance:

例如:

if (i = 0) {
    ...
}

is legal but probably a bug in an application that that is written C or C++.

是合法的,但可能是用 C 或 C++ 编写的应用程序中的错误。

Java avoids this and other problems by making booleanand the integer datatypes distinct types that cannot be converted from one to the other. Thus, the above is a compilation error in Java.

Java 通过使boolean整数数据类型成为不能从一种转换到另一种的不同类型来避免这个和其他问题。因此,以上是 Java 中的编译错误。

This doesn't work in all cases. For example (in Java):

这并不适用于所有情况。例如(在 Java 中):

if (flag = true) {
    ...
}

compiles. However, it does work in enough cases to be worthwhile. Furthermore, the idiomaticway to write the above in Java is:

编译。但是,它确实在足够多的情况下起作用,值得。此外,用 Java 编写上述内容的惯用方法是:

if (flag) {
    ...
}

which avoids the pitfall entirely. Also, a checker like findbugsor pmdshould flag the incorrect version as suspicious.

这完全避免了陷阱。此外,检查员喜欢findbugspmd应该将不正确的版本标记为可疑。



Now this doesn't explain why you cannot explicitly type casta booleanto an int. But I think that can be understood by observing the following:

现在这并不能解释为什么你不能显式地将类型转换为abooleanint。但我认为可以通过观察以下内容来理解:

  • You rarelyneed to do that in real Java programs.

  • Number <-> boolean casting wouldn't jell with the way that other type casts work. In particular, for other types there are up-casts and down-casts, and up-casts1in Java don't require an explicit type cast.

  • You can't typecast between numbers and string either, or between strings an other objects. These are conversions, not type casts. And int<-> booleanis too.

  • 很少需要做的是,在真正的Java程序。

  • 数字 <-> 布尔型转换不会像其他类型转换的工作方式一样。特别是,对于其他类型,有向上转换和向下转换,Java 中的向上转换1不需要显式类型转换。

  • 您也不能在数字和字符串之间进行类型转换,也不能在字符串与其他对象之间进行类型转换。这些是转换,而不是类型转换。和int< - >boolean过。



1 - I am being sloppy with my terminology here (deliberately). The correct Java terminology is "widening" and "narrowing". Narrowing conversions require an explicit type cast with a limited exception for literals. Widening conversions don't require a type cast.

1 - 我在这里(故意)对我的术语很草率。正确的 Java 术语是“扩大”和“缩小”。缩小转换需要显式类型转换,文字有限例外。扩大转换不需要类型转换。

回答by Andy Thomas

Java supports widening conversions on primitive numeric types. However, boolean is not considered a numeric type.

Java 支持原始数字类型的扩展转换。但是, boolean 不被视为数字类型。

The supported widening conversions are listed under "Widening Primitive Conversion"in the Java Language Specification.

支持的扩展转换列在Java 语言规范中的扩展基元转换”下。

回答by óscar López

Because Java is strongly typed, a booleanand an int are two completely different data typesand one can not be casted to the other - it can notbe casted, in fact.

因为 Java 是强类型的,aboolean和 anint are two completely different data types和一个不能转换为另一个 -事实上它不能被转换。