java: Use of Raw type as method parameter errases

2020-03-24 03:59发布

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.

标签: java generics
4条回答
家丑人穷心不美
2楼-- · 2020-03-24 04:11

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.

查看更多
成全新的幸福
3楼-- · 2020-03-24 04:12

"Doctor, it hurts when I do this."

Avoid raw types like the plague.

查看更多
该账号已被封号
4楼-- · 2020-03-24 04:16

+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);
查看更多
小情绪 Triste *
5楼-- · 2020-03-24 04:18

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

查看更多
登录 后发表回答