java: Use of Raw type as method parameter errases all parametrized type information in parameter members

后端 未结 4 1122
孤街浪徒
孤街浪徒 2021-01-13 11:47

Please explain me why if I use the raw type A in the method test() , the get() method on my typed list returns an Object and not a B.:

public class test
{
           


        
4条回答
  •  死守一世寂寞
    2021-01-13 12:16

    Ive been doing some digging around ang found this.

    http://java.sun.com/docs/books/tutorial/extra/generics/legacy.html

    Basically, erasure gets rid of (or erases) all generic type information. All the type information betweeen angle brackets is thrown out, so, for example, a parameterized type like List is converted into List. All remaining uses of type variables are replaced by the upper bound of the type variable (usually Object).

    By upper bound im taking it if they mean < T extends C>, C would be the upper bound, then as only B is the type varibable for aBlist then its upper bound would be Object.

    Anway hope this helps (Please dont mark me down again).

提交回复
热议问题