[英]Why Doesn't Java Allow Casting Boolean -> Int?
I was wondering why Java doesn't allow casting from a boolean to an int, like so: 我想知道为什么Java不允许从布尔值转换为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 double
and int
. 但似乎更好,更容易阅读的方式是允许类型转换,如
double
和int
。 Why doesn't Java include this feature? 为什么Java不包含此功能?
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 boolean
and the integer datatypes distinct types that cannot be converted from one to the other. Java通过使
boolean
和整数数据类型不能从一个转换为另一个的不同类型来避免这个和其他问题。 Thus, the above is a compilation error in Java. 因此,以上是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 idiomatic way to write the above in Java is:
此外,在Java中编写上述内容的惯用方法是:
if (flag) {
...
}
which avoids the pitfall entirely. 这完全避免了陷阱。 Also, a checker like
findbugs
or pmd
should flag the incorrect version as suspicious. 此外,像
findbugs
或pmd
这样的检查器应该将不正确的版本标记为可疑。
Now this doesn't explain why you cannot explicitly type cast a boolean
to an int
. 现在这并不能解释为什么你不能显式地将一个
boolean
类型转换为int
。 But I think that can be understood by observing the following: 但我认为可以通过观察以下内容来理解:
You rarely need to do that in real Java programs. 您很少需要在真正的Java程序中执行此操作。
Number <-> boolean casting wouldn't jell with the way that other type casts work. Number < - > boolean casting不会因其他类型转换的工作方式而崩溃。 In particular, for other types there are up-casts and down-casts, and up-casts 1 in Java don't require an explicit type cast.
特别是,对于其他类型,有向上转换和向下转换,Java中的向上转换1不需要显式类型转换。
You can't typecast between numbers and string either, or between strings an other objects. 您不能在数字和字符串之间进行类型转换,也不能在字符串之间进行类型转换。 These are conversions , not type casts.
这些是转换 ,而不是类型转换。 And
int
<-> boolean
is too. 并且
int
< - > boolean
也是。
1 - I am being sloppy with my terminology here (deliberately). 1 - 我在这里(故意)我的术语很草率。 The correct Java terminology is "widening" and "narrowing".
正确的Java术语是“扩大”和“缩小”。 Narrowing conversions require an explicit type cast with a limited exception for literals.
缩小转换需要显式类型转换,文字除外。 Widening conversions don't require a type cast.
扩展转换不需要类型转换。
Java supports widening conversions on primitive numeric types. Java支持扩展原始数字类型的转换。 However, boolean is not considered a numeric type.
但是,布尔值不被视为数字类型。
The supported widening conversions are listed under "Widening Primitive Conversion" in the Java Language Specification. 受支持的扩展转换列在Java语言规范中的“扩展原始转换”下。
因为Java是强类型的,所以boolean
和int are two completely different data types
,一种不能被转换为另一种 - 实际上它不能被转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.