Both code are same except only one(in bold) line I have added into 2nd code. Why the first code compiles? Why in print statement it takes list.get(0) without cast? But if I add String s=list.get(0), it gives me compiler error. Because "List list" is a non-generic, so the things comes out are Object type.
Now my question is , who is doing cast in print statement? Or I am missing something? can anyone please explain?
No one is doing a cast - PrintStream (which System.out is an instance of) contains both a void print(Object obj) as well as a void print(String s) method. So in the version of the code that compiles, it's calling the print(Object) version of the overloaded method. Hope that helps!
Author, Spring Security 3 (the Book), Packt Publishing, 2010