简体   繁体   English

Java向下转换

[英]Java Downcasting Conversion

Why does char c = 65 work and not throw an error but float d = 65.0 not work and throws a Static Error: Bad types in assignment: from double to float?为什么 char c = 65 有效而不抛出错误但 float d = 65.0 无效并抛出静态错误:分配中的错误类型:从双精度型到浮型? Aren't they both downcasting?两个人不都垂头丧气吗?

I could just quote something from the JLS section 5.2 and say "It works like this because the language spec says so", but that would not be satisfying would it?可以从引用一句JLS 5.2节,并说“这是这样的,因为语言规范是这么说的”,但不会满足不是吗?

My educated guess at why the designed the language like this:我对为什么设计这样的语言的有根据的猜测:

When converting 65 to a char , what you are really doing is just stripping off a bunch of insignificant 0s in the binary representation, since char is 16 bit, whereas int is 32 bit.65转换为char ,您真正要做的只是在二进制表示中去掉一堆无关紧要的 0,因为char是 16 位,而int是 32 位。 It's like turning 0065 to 65.这就像将 0065 变成 65。

On the other hand, 65.0 is a double and is represented in a floating point format.另一方面, 65.0是一个double 65.0值并且以浮点格式表示。 Note that generally, this representation is only an approximation of the true value.请注意,通常,这种表示只是真实值的近似值。 See this post for more info.有关更多信息,请参阅此帖子 The more bits, the better the approximation.位数越多,近似效果越好。 So converting from double (64 bits) to float (32 bits) is kind of like converting 0.333333 to 0.333.因此,从double (64 位)转换为float (32 位)有点像将 0.333333 转换为 0.333。 Hopefully you'd agree that this is a greater loss of information than turning 0065 to 65. Therefore, this is not a conversion that should be a performed automatically.希望您同意,这比将 0065 转换为 65 损失的信息更大。因此,这不是应该自动执行的转换。 Programmers need to be aware that this is happening.程序员需要意识到这种情况正在发生。


Anyway, here's the relevant part of the language spec, for completeness's sake:无论如何,为了完整起见,这是语言规范的相关部分:

Assignment contexts allow the use of one of the following:赋值上下文允许使用以下之一:

... ...

In addition, if the expression is a constant expression (§15.28) of type byte , short , char , or int :此外,如果表达式是byteshortcharint类型的常量表达式(第 15.28 节):

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.如果变量的类型是 byte、short 或 char,并且常量表达式的值可以在变量的类型中表示,则可以使用缩小原语转换。

So the language spec kind of gives byte, short, char and int special treatment, allowing them a narrowing conversion in an assignment context.因此,语言规范对 byte、short、char 和 int 进行了特殊处理,允许它们在赋值上下文中进行缩小转换。 Do note that the expression must be:请注意,表达式必须是:

  • a constant expression 常量表达式
  • within the range of the target type, so char x = 100000;在目标类型的范围内,所以char x = 100000; is invalid.是无效的。
float d = 65.0;

65.0 is not a float value. 65.0 不是浮点值。 It is a double value.这是一个双重价值。 When entering float values, the number must end with the letter f.输入浮点值时,数字必须以字母 f 结尾。

You see here.你看这里。

float d = 65.0f;

If you want to give d to 65 without having to write the letter f, you can do the following.如果你想给 d 给 65 而不必写字母 f,你可以执行以下操作。

float d = (float)65.0;

This is known as narrow casting.这被称为窄铸造。

65.0 in Java-sense is a double, because of the . Java 意义上的65.0是双倍的,因为. . . If you want this number to be a float, just add f behind it.如果你希望这个数字是一个浮点数,只需在它后面添加f

float d = 65.0f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM