Java中的原始转换和赋值 [英] Primitive cast and assignments in Java

查看:82
本文介绍了Java中的原始转换和赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解为什么以下错误:

I understand why the following is wrong:

byte a = 3; 
byte b = 8; 
byte c = a + b;  // compile error

它不会编译.表达式始终导致int.因此,我们应该进行显式转换:

It won't compile. Expressions always result in an int. So we should have done the explicit cast:

byte c = (byte) (a + b);   // valid code

但是我不明白为什么以下情况是正确的:

byte d = 3 + 8;   // it's valid! why?

因为文字整数(例如3或8)始终隐式为int.并且int-or-smaller表达式也总是会导致int.有人可以解释这里发生了什么吗?

Because literal integer (such as 3 or 8) is always implicitly an int. And int-or-smaller expressions always result in an int too. Can anybody explain what is going on here?

我唯一能猜到的是编译器将此表达式等同于以下内容:

The only thing I can guess is that the compiler equates this expression to the following:

byte d = 11;

并且不认为这是一个表达式.

and doesn't consider this an expression.

推荐答案

less†3 + 8是否在编译时被评估为11有关,而 more 与以下事实有关:明确允许编译器在某些情况下将int s隐式缩小为byte s.特别是,语言规范明确允许在编译时将int类型的常量表达式隐式变窄为byte的常量,而这些常量表达式可以适合byte.

This has less† to do with whether or not 3 + 8 is evaluated to 11 at compile-time, and more to do with the fact the compiler is explicitly permitted to implicitly narrow ints to bytes in certain cases. In particular, the language specification explicitly permits implicit narrowing conversions to byte of constant expressions of type int that can fit in a byte at compile-time.

JLS的相关部分在此处是第5.2节:

此外,如果该表达式是类型为byteshort的常量表达式(第15.28节), charint:

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

  • 如果 变量为byteshortchar,并且常量的值 表达式可以用变量的类型表示.
  • 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 theAnswer = 42; 被允许.如果不进行缩小,则整数文字42具有类型int的事实将 表示将强制转换为byte:

The compile-time narrowing of constants means that code such as: byte theAnswer = 42; is allowed. Without the narrowing, the fact that the integer literal 42 has type int would mean that a cast to byte would be required:

†:显然,根据规范,需要对常量表达式求值,以查看其是否适合较窄的类型. 但是要注意的一点是,如果没有本规范的这一部分,将不允许编译器进行隐式变窄转换..

†: Obviously, as per the specification, the constant expression needs to be evaluated to see if it fits in the narrower type or not. But the salient point is that without this section of the specification, the compiler would not be permitted to make the implicit narrowing conversion.

让我们在这里明确

byte a = 3; 
byte b = 8; 

允许这些 的原因是由于规范的上述部分.即,允许编译器将文字3隐式变窄为byte.这不是因为编译器在编译时将常量表达式3评估为其值3.

The reason that these are permitted is because of the above section of the specification. That is, the compiler is allowed to make the implicit narrowing conversion of the literal 3 to a byte. It's not because the compiler evaluates the constant expression 3 to its value 3 at compile-time.

这篇关于Java中的原始转换和赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆