6

I have a generic method declared as below

public static <E extends Number> List<E> myListOfElements(List<E> list) {
        return new ArrayList<Integer>();
}

What I understand from this is it's expecting a List of type which extends Number as input and List of type which extends Number as output. When I try to return an ArrayList<Integer>r it gives a compilation error

Type mismatch: cannot convert from ArrayList<Integer> to List<E>

I don't understand what is the problem here. Why can't I return an ArrayList<Integer> here?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Abhilash
  • 803
  • 1
  • 9
  • 32
  • Did you import the right `List` (`java.util.List`)? – MadProgrammer May 23 '18 at 04:16
  • 6
    The type variable `E` is universally quantified. This means that a method that implements that type signature must be able to return a `List` *for all* `E` extending `Number`, not merely for `E = Integer`. – Chris Martin May 23 '18 at 04:17
  • FYI: Anything that looks like XML (i.e. ``) must be code-formatted (surrounded by back-ticks or the entire block indented 4 spaces, or it will get swallowed by Markdown processing. – Jim Garrison May 23 '18 at 04:17
  • You're returning an `ArrayList`, but you could invoke this with `List list = myListOfElements(Arrays.asList(0.0));`. – Andy Turner May 23 '18 at 04:18
  • @ChrisMartin If you expand on your comment a little you have a good answer. – Jim Garrison May 23 '18 at 04:19
  • 1
    @Code-Apprentice this dupe is relevant, but not directly: OP isn't trying to assign a `List` to a `List`, but rather a `List` to a `List`. – Andy Turner May 23 '18 at 04:21
  • @AndyTurner Yah, I may have jumped the gun with that dupe. Do you have a better suggestion for a dupe or should I reopen? – Code-Apprentice May 23 '18 at 04:22
  • @Code-Apprentice [This one](https://stackoverflow.com/questions/40809872/java-generics-type-mismatch-cannot-convert-from-integer-to-k) is likely better. Sorry. – Logan May 23 '18 at 04:26
  • @Logan You don't need to apologize for anything. – Code-Apprentice May 23 '18 at 04:27
  • @Code-Apprentice Well I jumped the gun a bit there with the suggested dupe, haha. – Logan May 23 '18 at 04:28
  • @AndyTurner If we cast it to `(List)` in the method, why doesn't the typecast fail in that method? Example: If I do `List myD = myListOfElements(new ArrayList());` and the method actually retunrs `new ArrayList()` – Thiyagu May 23 '18 at 04:28
  • @user7 Can you show a complete example? You can link to http://repl.it or some similar site. – Code-Apprentice May 23 '18 at 04:29
  • @user7 "why doesn't the typecast fail in that method" What do you mean? Are you asking why you can write that code in the first place? – Andy Turner May 23 '18 at 04:30
  • @Code-Apprentice Here it is https://ideone.com/G37mYP – Thiyagu May 23 '18 at 04:31
  • @AndyTurner Please refer to the ideone link for the sample code – Thiyagu May 23 '18 at 04:31
  • Has it to do with the *unchecked cast* – Thiyagu May 23 '18 at 04:31
  • @AndyTurner In `myListOfElements(new ArrayList());` `E` gets inferred as `Integer` - then in the method aren't we casting `ArrayList` to `List`? – Thiyagu May 23 '18 at 04:32
  • @user7 yes, the unchecked cast is relevant. That's saying that the cast isn't actually a cast in the compiled code, so it doesn't fail at that point, but rather later, when you attempt to use the element as an `Integer`. – Andy Turner May 23 '18 at 04:35
  • @AndyTurner I guess with cast we get away at compile time. At runtime due to type erasure, everything is a just a raw type List - so failure happens only we retrieve from list. Am I right? – Thiyagu May 23 '18 at 04:37
  • @user7 By explicitly casting in `return (List) i;`, you are telling the compiler that you think you know what you are doing. Without that cast, you should get a similar error to the one asked about here. – Code-Apprentice May 23 '18 at 04:40
  • Something like [this](https://repl.it/@codeguru/UnfoldedDodgerblueCable) will compile, but I don't think it enforces that the two captured types are the same. – Code-Apprentice May 23 '18 at 04:42
  • @Code-Apprentice Thanks for the inputs. – Thiyagu May 23 '18 at 04:55

0 Answers0