【问题标题】:Why can not I add two bytes and get an int and I can add two final bytes get a byte?为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节?
【发布时间】:2012-10-17 11:48:25
【问题描述】:
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);
    }
}

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

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

【问题讨论】:

    标签: java int variable-assignment scjp ocpjp


    【解决方案1】:

    From the JLS 5.2 Assignment Conversion

    此外,如果表达式是 byte、short、char 或 int 类型的常量表达式(第 15.28 节): - 如果类型为 变量是 byte、short 或 char,以及常量的值 表达式可以用变量的类型来表示。

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

    考虑你的表达方式

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

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

    如果你这样做了

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

    【讨论】:

    【解决方案2】:
    byte z = x + y;  // x and y are declared final
    

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

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

    然而,在这种情况下,ab 的值未声明为最终值。因此,表达式的值在编译时是未知的,而是在运行时评估的。因此,您需要进行显式强制转换。


    但是,即使在第一个代码中,如果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`
    

    【讨论】:

    • 如果 x = 120 和 y = 120 会追加什么?
    • @Aubin。在这种情况下,它会失败。因为 240 不能容纳在字节中。
    【解决方案3】:

    每当我们在两个变量 a 和 b 之间执行任何算术运算时,结果总是,

    max(int, type of a, type of b)
    
    byte a=10;
    byte b=20;
    byte c=a+b(C.E )
    

    解释:如上所述 max(int, a 的类型, b 的类型)

    最大(整数,字节,字节)

    结果是类型:int,找到的是int,但需要在字节中

    所以我们需要要求类型转换为字节

        byte a=10;
        byte b=20;
        byte c=(byte) (a+b);
    

    【讨论】:

    • 您能解释一下为什么我们必须将 a,b 的总和转换为字节为byte c=(byte) (a+b);,为什么会抛出错误? byte c=(a+b);?
    猜你喜欢
    • 2013-07-09
    • 2021-05-10
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2015-03-30
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多