0

I've always got these warnings in IntelliJ IDEA about unparametrized classes because I usually type List a = getList() and typing List<?> a = getList() just fixes the warning. But I'm unsure about how exactly the wildward works and if this might end up in some unexpected errors. Is it really equivalent and I can go List<?> without worrying?

Michael
  • 41,989
  • 11
  • 82
  • 128
RabidTunes
  • 775
  • 1
  • 8
  • 21
  • No, they aren't equivalent: try adding a string to each, see what the difference is. `List>` is the safe one, and thus the one you should use. – Andy Turner May 06 '21 at 14:31
  • 2
    The raw List is basically only supported because of backwards compatibility reasons. Before generics were added, that's how all lists looked. Now, you should specify a type. If you genuinely don't care about the type of items stored in the list, then you can use `?`. They are not precisely identical, but they are close. For one, using `?` makes it clear that you considered what type it should be and concluded that it doesn't need to be anything specific. Using the raw List could mean that you just forgot, or that the code was written before Java 5 or something – Michael May 06 '21 at 14:31
  • Thanks for the clarification! I wish I could mark this comment as the answer – RabidTunes May 06 '21 at 16:20

0 Answers0