• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Wildcards  RSS feed

 
Brendon McCullum
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, this is my first post on Java Ranch. I have a very small doubt, regarding Java wildcards.
I am going through the following documentation on wildcards : Javadocs

All's clear but for a certain piece of code :


Here as per the author, the type of c is unknown, hence a new Object cannot be added to it, since the type is not known. But, as per first line of this code, the collection refers to an ArrayList of Strings.
Hence, isn't the type String defined for the Collection c. If not, what does line 1 infers to?
I would appreciate help on this. Thanks.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can never remember why, but you can never add to a Collection<?>. The fact that it is declared as Collection<?> is what matters, not that its runtime type is Collection<String>.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got to be off now, but


… welcome to the Ranch
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brendon McCullum wrote:Here as per the author, the type of c is unknown, hence a new Object cannot be added to it, since the type is not known. But, as per first line of this code, the collection refers to an ArrayList of Strings.
Hence, isn't the type String defined for the Collection c. If not, what does line 1 infers to?
I would appreciate help on this. Thanks.

Yes your first line does assign an ArrayList of Strings to 'c' but between that line and and line where a String is being added to 'c' a different Collection (eg ArrayList<Integer>) could have been assigned to 'c'. So just because you originally assigned an ArrayList of Strings to 'c' it doesn't mean it will always refer to an ArrayList of Strings. If you want 'c' to always refer to a Collection of Strings then declare 'c' as Collection<String> rather than using the wildcard.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think generics is too advanced a topic for the beginner's forum; I've promoted this to the General Java forum.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brendon McCullum wrote:
Here as per the author, the type of c is unknown, hence a new Object cannot be added to it, since the type is not known. But, as per first line of this code, the collection refers to an ArrayList of Strings.
Hence, isn't the type String defined for the Collection c. If not, what does line 1 infers to?


Simply, there is nothing in the Java Specification that says, the compiler should go back a few lines and try to figure out what type it is. And if it did, how? how far should it go back? What to do if that value is not really clear? etc. etc. etc.

It is simply an ArrayList of an unknown type. Period.


Oh, and BTW ... you can't add an Object instance into an ArrayList<String>, so it would be a compile error, even if the compiler can figure it out.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I can never remember why, but you can never add to a Collection<?>. The fact that it is declared as Collection<?> is what matters, not that its runtime type is Collection<String>.


Technically, there is one value that can be added to a collection of any generic type, and hence, can be added to a collection of an unknown generic type. And that value is null.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!