• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java Wildcards

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79977
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79977
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got to be off now, but


… welcome to the Ranch
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic