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

青春壹個敷衍的年華 提交于 2020-04-06 07:34:26

问题


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
{
    public class B{}
    public class C{}

    public class A<T extends C>
    {
        private List<B> aBList;

        public List<B> mGetBList()
        {
            return aBList;
        }
    }

    public test(A pA) // Use of raw type - this is bad, I know!
    {
        B lB = pA.mGetBList().get(0); // Compile error: Type mismatch: 
                                      // cannot convert from Object to test.B   

    }
}

If I declare

public test(A<?> pA)

the get() method returns a B as expected.


回答1:


+1 for interesting test case.

Looks like erasure erases everything, so in this case, you end up with.

public List mGetBList()

And erasure of List will result in public Object get( int ), which, of course, cannot be assigned to B.

If there is no strong need for raw type in method signature, use generic form that you have provided, otherwise cast the object to B

B lB = (B) pA.mGetBList().get(0);



回答2:


"Doctor, it hurts when I do this."

Avoid raw types like the plague.




回答3:


When you decalare your argument pa in the method test, no parameter is included, (including wildcard ). Therefore the list is held in a variable which contains a list of objects, so when you try to extract an element from the list you get a object, so you would have to cast it back to the required type, in this case B.

When using a wildcard the compliler is told not promote the list to a varibale containing an list holding Object. It is told that the contents are of the list are of 'unknown type' and is to be left alone. It is then upto the programmer to ensure that when extracting an element it is assigned to a suitable varibale, without using a cast.




回答4:


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).



来源:https://stackoverflow.com/questions/1675243/java-use-of-raw-type-as-method-parameter-errases-all-parametrized-type-informat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!