简体   繁体   English

带有“表达式”的Java类型提升

[英]Java type promotion with “expressions”

Can the variable below (called b) be called an expression, if it is the only thing located to right of the equals sign? 如果下面的变量(称为b)是唯一位于等号右边的变量,它可以称为表达式吗?

// This code fragment will not compile.
// c is a char, and b is a byte.

c = b;

The reason why I ask this question is because of the topic of type promotion within expressions. 我之所以问这个问题,是因为表达式中的类型提升这一主题。 I understand that Java promotes all bytes to ints. 我知道Java会将所有字节提升为整数。 Is that the only reason why this code fragment does not compile? 这是该代码片段无法编译的唯一原因吗? (Please note that I know about casts; that's not the point of this thread. Thanks a lot.) (请注意,我了解演员表;这不是该主题的重点。非常感谢。)

Edit: Thanks a lot Jon and Peter. 编辑:非常感谢乔恩和彼得。 Looking at this topic using a second example: 使用第二个示例查看此主题:

byte b = 1;
short s = 2;

s = b;  // OK
s = b*2;  // Not OK (compilation error)

Is it true that the following is happening? 是否确实发生了以下情况?

(Line 3) Java converts the byte to short. (第3行)Java将字节转换为short。 (Line 4) Java converts the expression b*2 to an int. (第4行)Java将表达式b * 2转换为int。

If this is correct, then it would seem that =b; 如果这是正确的,那么看来= b; and =b*2; 和= b * 2; are "expressions" that Java handles differently. 是Java处理不同的“表达式”。 So, the =b; 因此,= b; "expression" is not converted to an int, but widened to a short. “表达式”不转换为整数,而是扩展为短整数。 But the =b*2; 但是= b * 2; expression is converted to an int, and not to a short, even though the target variable called s is a short. 即使称为s的目标变量是short,表达式也会转换为int而不是short。

Edit 2: Also - 编辑2:也是-

short s1, s2 = 2, s3 = 2;
s1 = s2*s3;  // Not OK (compilation error)

Even though all three variables are shorts, the s2*s3; 即使所有三个变量都是短裤,s2 * s3; expression is promoted to an int, and thus causes a compilation error. 表达式提升为int,从而导致编译错误。

Try this 尝试这个

byte b = -1;
short s = b; // is ok as a byte can be promoted to an short.
int i = b; // is ok as a byte can be promoted to an int.
float f = b; // is ok as a byte can be promoted to an float, long or double.

char c = b; // won't compile

but

final byte b = 1;
char c = b; // compiles fine as the compiler can inline the value.

In this case 在这种情况下

short s = b*2;  // Not OK (compilation error)

b * 2 is an int as 2 is an int value. b * 2是一个int因为2是一个int数值。 If b were final you would be able to do this was the compile can inline the value. 如果b是最终的,则可以执行此操作,因为编译可以内联该值。

Can the variable below (called b) be called an expression, if it is the only thing located to right of the equals sign? 如果下面的变量(称为b)是唯一位于等号右边的变量,它可以称为表达式吗?

Yes, absolutely. 是的,一点没错。

I understand that Java promotes all bytes to ints. 我知道Java会将所有字节提升为整数。

Well, in some cases. 好吧,在某些情况下。 Not in all cases. 并非在所有情况下都如此。

Fundamentally the code doesn't compile because there's no implicit conversion from byte to char . 从根本上讲,该代码不会编译,因为没有从bytechar隐式转换。 From section 5.1.2 of the JLS (widening primitive conversions): JLS的5.1.2节 (扩展原始转换)开始:

19 specific conversions on primitive types are called the widening primitive conversions: 关于原始类型的19种特定转换称为扩展原始转换:

  • byte to short , int , long , float , or double byteshortintlongfloatdouble
  • ... ...

Note the lack of char in the list of target types for conversions from byte . 请注意,从byte转换的目标类型列表中缺少char

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

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