Usually For the Collections, if the type information is not specified- The compiler gives a warning( after Generics were introduced). So always preferred is to use Type information along with the Collections.
At runtime, there is no generics whatsoever. Generics provides type safety only at compile time. So if I write this code, compiler will warn me at compile time but it will run fine
Now the compiler warns you when assigning a raw collection to a typed one because the compiler knows that it can break type safety. Like this code will break type safety
Hi Ankit, Your explanation is good. My question is different.
In your example on Left Hand side, there is no typesafe code. But, in my example there is type safety in both cases on left side. Maybe I need to change the question. What is the difference between
1. typesafety defined on Left hand side and type safety NOT DEFINED on right hand side
2. typesafety defined on Left hand side and type safety DEFINED on right hand side
I feel both are absolutely same during compile time and runtime in functionality wise(I mean code will not break in any case). Even though we get compile WARNING for Option 1, It should not be matter. Please correct me if I am wrong.
Saibabaa, my 2nd code uses a type safe collection on the left hand side and a raw collection on the right hand side (line 3). If you are creating a new collection, then there is no difference in both the scenarios that you mentioned
Both will work the same in this case (except for warning on line 1). The reason the compiler issues a warning at the 1st line is explained in my previous post. The compiler is only concerned that you are assigning a raw collection to a type safe collection at line 1. In this case we just created the raw collection and it doesn't have any other reference so type safety can't be broken but it can be broken as I showed in the example...