This Java code
public class test{
public static void main(String[] args){
byte[] a = new byte[1];
a[0] = 1;
byte x = 1;
x = x + a[0];
System.out.println(x);
}
}
throws the following compile error:
test.java:10: possible loss of precision
found : int
required: byte
byte y = x + a[0];
^
1 error
huh? What is going on here? all variables are declared as byte. Explicitly casting the 1's to byte doesn't make any difference. However, change to
public class test{
public static void main(String[] args){
byte[] a = new byte[1];
a[0] = 1;
byte x = 1;
x += a[0];
System.out.println(x);
}
}
and everything compiles fine. I'm compiling with java version 1.6.0_16, build-b01. My questions are: Is this a bug or a feature? Why does += perform differently than + ?