java 为什么我不能添加两个字节并获得一个 int 而我可以添加两个最终字节获得一个字节?

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

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

javaintvariable-assignmentscjpocpjp

提问by Joe

public class Java{
    public static void main(String[] args){
        final byte x = 1;
        final byte y = 2;
        byte z = x + y;//ok
        System.out.println(z);

        byte a = 1;
        byte b = 2;
        byte c = a + b; //Compiler error
        System.out.println(c);
    }
}

If the result of an expression involving anything int-sized or smaller is always an int even if the sum of two bytes fit in a byte.

如果涉及任何 int 大小或更小的表达式的结果始终是 int,即使两个字节的总和适合一个字节。

Why does it happen when we add two final bytes that fit in a byte?There is no compiler error.

当我们添加适合一个字节的两个最终字节时,为什么会发生这种情况?没有编译器错误。

回答by Amit Deshpande

From the JLS 5.2 Assignment Conversion

来自 JLS 5.2 作业转换

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: - 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 或 int 类型的常量表达式(第 15.28 节): - 如果变量的类型是 byte、short 或 char,并且值常量表达式的类型可以用变量的类型来表示。

In short the value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable that is byte.

简而言之,表达式的值(在编译时已知,因为它是一个常量表达式)可以用字节变量的类型来表示。

Consider your expression

考虑你的表情

 final byte x = 1;
 final byte y = 2;
 byte z = x + y;//This is constant expression and value is known at compile time

So as summation fits into byte it does not raise an compilation error.

因此,当求和适合字节时,它不会引发编译错误。

Now if you do

现在如果你这样做

final byte x = 100;
final byte y = 100;
byte z = x + y;// Compilation error it no longer fits in byte

回答by Rohit Jain

byte z = x + y;  // x and y are declared final

Here, since xand yare declared finalso the value of expression on the RHSis known at compile time, which is fixed at (1 + 2 = 3)and cannot vary. So, you don't need to typecast it explicitly

在这里,因为xy被声明final所以表达式的值RHS在编译时是已知的,它是固定的(1 + 2 = 3)并且不能改变。所以,你不需要明确地对它进行类型转换

byte c = a + b;   // a and b are not declared final

Whereas, in this case, value of aand bare not declared final. So, the value of expression is not known at compile time, rather is evaluated at runtime. So, you need to do an explicit cast.

而在这种情况下,a和 的值b未声明为最终值。因此,表达式的值在编译时是未知的,而是在运行时计算的。因此,您需要进行显式转换。



However, even in the 1st code, if the value of a + bcomes out to be outside the range -128 to 127, it will fail to compile.

但是,即使在第一个代码中,如果 的值a + b超出范围-128 to 127,它也将无法编译。

final byte b = 121;
final byte a = 120;
byte x = a + b;  // This won't compile, as `241` is outside the range of `byte`

final byte b1 = 12;
final byte a1 = 12;
byte x1 = a1 + b1;  // Will Compile. byte can accommodate `24`