Java generics. Type mismatch: cannot convert from object to

user3803250

I have the following generic Java class:

class A<T> {
    List<A> list;

    List<A> getList() {
        return list;
    }
}

When I try to get the first element of the list

A a = new A();
A b = a.getList().get(0);

I have the compiler error:

Type mismatch: cannot convert from Object to A

If I introduce an intermediary list:

A a = new A();
List<A> sameList = a.getList();
A b = sameList.get(0);

the error disappears, but the warning appears:

Type safety: The expression of type List needs unchecked conversion to conform to List<A>

The variable sameList has the same type as the method getList(). I am ready to put up with it, but I want to understand the reason. Why is there is a conversion?

awksp

This is one of the fun parts about raw types. Not only do you not get generic checks on the raw type itself, but it also causes generic checks for all interacting code to be skipped too.

Thus, when you use the raw type A, the List<A> you defined inside A is actually erased to List, so that getList returns a raw type List, and get(0) would return an Object.

This is also why you get an "unchecked conversion" warning. getList() returns a List, not a List<A>. So assigning a raw type List to a List<A> will produce an unchecked conversion warning.

The variable sameList has the same type as the method getList()

So you're actually wrong, here. sameList has a different type as the return type of getList() for the raw type A.

To solve this you'll need to parameterize all uses of A in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type mismatch: cannot convert from long to int

From Dev

Type mismatch: cannot convert from java.lang.String to String

From Dev

type mismatch cannot convert from element type object to string

From Dev

Type mismatch: cannot convert from element type Object to Cookie

From Dev

Java - Type mismatch: cannot convert from element type Object to String

From Dev

Type mismatch: cannot convert from Object to Class object

From Dev

Java Generics cannot convert Type in Type

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Type mismatch: cannot convert from boolean to int

From Dev

Type mismatch: cannot convert from ListFragment to Fragment

From Dev

Type mismatch cannot convert from String to String[]

From Dev

Java generics - type mismatch from T to T

From Dev

issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

From Dev

type mismatch: cannot convert from double to Double

From Dev

Java collection Type mismatch: cannot convert from TreeSet to SortedSet

From Dev

Type mismatch: cannot convert from Optional<Object> to BasketDTO

From Dev

spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

From Dev

Java generics : Type mismatch: cannot convert from Integer to K

From Dev

error : Type mismatch: cannot convert from Object to JSONObject

From Dev

type mismatch cannot convert from element type object to string

From Dev

Type mismatch: cannot convert from Scanner to boolean

From Dev

Type mismatch: cannot convert from element type Object to Cookie

From Dev

Type mismatch: cannot convert from void to Integer

From Dev

Java - Type mismatch: cannot convert from element type Object to String

From Dev

Type mismatch: cannot convert from Object to Class object

From Dev

Type mismatch: cannot convert from int to TextView

From Dev

Type mismatch: cannot convert from Set<Object> to Set<Long>

From Dev

Type mismatch on java and cannot instantiate the type of object

From Dev

Type mismatch: cannot convert from element type Object to List

Related Related

  1. 1

    Type mismatch: cannot convert from long to int

  2. 2

    Type mismatch: cannot convert from java.lang.String to String

  3. 3

    type mismatch cannot convert from element type object to string

  4. 4

    Type mismatch: cannot convert from element type Object to Cookie

  5. 5

    Java - Type mismatch: cannot convert from element type Object to String

  6. 6

    Type mismatch: cannot convert from Object to Class object

  7. 7

    Java Generics cannot convert Type in Type

  8. 8

    Java generics. Type mismatch: cannot convert from object to

  9. 9

    Type mismatch: cannot convert from boolean to int

  10. 10

    Type mismatch: cannot convert from ListFragment to Fragment

  11. 11

    Type mismatch cannot convert from String to String[]

  12. 12

    Java generics - type mismatch from T to T

  13. 13

    issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

  14. 14

    type mismatch: cannot convert from double to Double

  15. 15

    Java collection Type mismatch: cannot convert from TreeSet to SortedSet

  16. 16

    Type mismatch: cannot convert from Optional<Object> to BasketDTO

  17. 17

    spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

  18. 18

    Java generics : Type mismatch: cannot convert from Integer to K

  19. 19

    error : Type mismatch: cannot convert from Object to JSONObject

  20. 20

    type mismatch cannot convert from element type object to string

  21. 21

    Type mismatch: cannot convert from Scanner to boolean

  22. 22

    Type mismatch: cannot convert from element type Object to Cookie

  23. 23

    Type mismatch: cannot convert from void to Integer

  24. 24

    Java - Type mismatch: cannot convert from element type Object to String

  25. 25

    Type mismatch: cannot convert from Object to Class object

  26. 26

    Type mismatch: cannot convert from int to TextView

  27. 27

    Type mismatch: cannot convert from Set<Object> to Set<Long>

  28. 28

    Type mismatch on java and cannot instantiate the type of object

  29. 29

    Type mismatch: cannot convert from element type Object to List

HotTag

Archive