我有以下Java代码的问题

San*_*eev 3 java

public class b {
    public static void main(String[] args) {
        byte b = 1;
        long l = 127;
    //  b = b + l;            // 1 if I try this then it does not compile
        b += l;               // 2 if I try this then it does     compile
        System.out.println(b);  
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用此代码,但我有问题:我不明白为什么b=b+l;不编译,但如果我写,b+=l;那么它编译并运行.

请解释为什么会这样.

Inv*_*r53 13

b+=1用Java自动输入类型; b=b+1才不是.

  • 另见http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2 (3认同)