I’m just curious what’s the generated inferred T type when passing a raw to a T type. It does compile but with a warning.
public class GenericMethodInference {
static <T> void test5(List<T t>){} // clause (5)
public static void main(String [] args) {
List e = new ArrayList<Integer>();
test5(e); // clause (6) // ok but with warning.
}
Assigning a raw type to a generic type will assume no generic constraint (which will default to Object) but remove any compile-time type safety guarantees that might otherwise have existed.
It sounds like there is a fundamental misunderstanding of type erasure here. The raw type is the end result of a generic type. You specified
List<Integer>, the compiler verifies against that type information and then removes the<Integer>portion of that, inserting casts to Integer as appropriate from actions against the raw type of List.That means that these two retrieval operations are exactly the same.
For example, the following is legal. This only gives a warning (instead of causing a compiler error or runtime error) because we’re acting on Object and are still within the appropriate bounds of the contained type.
It will print out 5. Where the type system helps is if you try to do something like this.
It will give you a compiler error stating that there is a type mismatch. Using the raw type directly, however, will throw all of that type safety away and rely solely on your judgement for the correct types that should be used. This allows you to easily shoot yourself in the foot if you get it wrong.