Can anybody explain the difference between the following two programs? They look equivalent to me but they generate different outputs. What's the reason?
Program 1: Outputs incorrect value; i=1
public class Test1{
public static void main(String[] args) {
int[] values = new int[] { 2, 3, 5, 1 };
int i = 0;
for (Integer integer : values) {
i =+ integer.intValue();
} // for loop ends
System.out.println("i=" + i);
}
}
Program 2: Outputs expected value; i=11"
public class Test2{
public static void main(String[] args) {
int[] values = new int[] { 2, 3, 5, 1 };
int i = 0;
for (Integer integer : values) {
i = i + integer.intValue();
} // for loop ends
System.out.println("i=" + i);
}
}