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

后端 未结 4 1025
孤街浪徒
孤街浪徒 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:04

    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.

    0 讨论(0)
  • 2021-01-13 12:08

    "Doctor, it hurts when I do this."

    Avoid raw types like the plague.

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-01-13 12:20

    +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);
    
    0 讨论(0)
提交回复
热议问题