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

Java generics. Type mismatch: cannot convert from object to

From Dev

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

From Dev

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

From Dev

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

From Dev

Type mismatch: cannot convert from element type Object to Cookie

From Dev

type mismatch cannot convert from element type object 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

Type mismatch: cannot convert from element type Object to List

From Dev

Type mismatch: cannot convert from Object to Class object

From Dev

Type mismatch: cannot convert from Object to Class object

From Dev

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

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

error : Type mismatch: cannot convert from Object to JSONObject

From Dev

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

From Dev

Java generics - type mismatch from T to T

From Dev

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

From Dev

Java collection Type mismatch: cannot convert from TreeSet to SortedSet

From Dev

Java Generics cannot convert Type in Type

From Dev

Type mismatch on java and cannot instantiate the type of object

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 long to int

From Dev

type mismatch: cannot convert from double to Double

From Dev

Type mismatch cannot convert from String to String[]

From Dev

Type mismatch: cannot convert from Scanner to boolean

From Dev

Type mismatch: cannot convert from void to Integer

From Dev

Type mismatch: cannot convert from int to TextView

Related Related

  1. 1

    Java generics. Type mismatch: cannot convert from object to

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    Type mismatch: cannot convert from element type Object to Cookie

  6. 6

    type mismatch cannot convert from element type object to string

  7. 7

    type mismatch cannot convert from element type object to string

  8. 8

    Type mismatch: cannot convert from element type Object to Cookie

  9. 9

    Type mismatch: cannot convert from element type Object to List

  10. 10

    Type mismatch: cannot convert from Object to Class object

  11. 11

    Type mismatch: cannot convert from Object to Class object

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    error : Type mismatch: cannot convert from Object to JSONObject

  16. 16

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

  17. 17

    Java generics - type mismatch from T to T

  18. 18

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

  19. 19

    Java collection Type mismatch: cannot convert from TreeSet to SortedSet

  20. 20

    Java Generics cannot convert Type in Type

  21. 21

    Type mismatch on java and cannot instantiate the type of object

  22. 22

    Type mismatch: cannot convert from boolean to int

  23. 23

    Type mismatch: cannot convert from ListFragment to Fragment

  24. 24

    Type mismatch: cannot convert from long to int

  25. 25

    type mismatch: cannot convert from double to Double

  26. 26

    Type mismatch cannot convert from String to String[]

  27. 27

    Type mismatch: cannot convert from Scanner to boolean

  28. 28

    Type mismatch: cannot convert from void to Integer

  29. 29

    Type mismatch: cannot convert from int to TextView

HotTag

Archive