While assisting at Mark Reinhold talk on JDK 7 updates at devoxx, it reminded me a problem we had very recently in a project.
Here is a small piece of java code:
import java.util.ArrayList;
import java.util.List;
public class TestForEachLoop {
public static void main(String[] args) {
List<String> listOfStrings = new ArrayList<String>();
doSomethingWithListInPreJava5(listOfStrings);
for (Object o : listOfStrings) {
System.out.println(o);
}
}
private static void doSomethingWithListInPreJava5(List list) {
list.add(new Integer(12));
}
}
So, do you have an idea of what will happen if I compile and run this piece of code? I'll let you comment if you have an idea, and I promise the final answer is not obvious!
