public static void main(String args[]) {
byte a = 3;
byte b = 2;
b = a + b;
System.out.println(b);
public static void main(String args[]) {
byte a = 127;
byte b = 126;
b = a + b;
System.out.println(b);
}
虽然我们都知道byte在运算时会被自动的转换为int类型
也就是说(a+b)的类型为int
但是左边的 b=a+b 这个b的类型还是为byte
你要想b=a+b成立 想必大家都知道
b=(byte)a+b
public static void main(String args[]) {
byte a = 3;
byte b = 2;
a+=b;
System.out.println(b);
}
-----------------------------------------
public static void main(String args[]) {
byte a = 127;
byte b = 127;
a+=b;
System.out.println(b);
}
这里有个小知识点:
a+=b; 对于这类运算,系统会自动的帮我们转换类型
a+=b 实际上启动帮我们转换为------> a=(a的类型)(a+b)
所以 对于 += -= *= /= ......系统都会自动帮我们做类型转换