Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Jeanne Boyarsky and Scott Selikoff OCP book  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a little question about generics.

On p119 i modify the code below


by this code


And the answer is :
java
java
1
1

Now here is my question: what is the need of the Unbounded Wildcards?
I mean, in which case we must use the Unbounded Wildcard instead of a generic method?
And in which case we must use a generic method instead of an unboundwildcard.

In my point of view, regarding the above example, they do the same thing.
but generic method seems to be more readable.

Thank you for your answer.

regards
Vincent



 
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unbounded wildcard is immutable. So you can not add any element to List<?> list of printList(List<?> list). You can not add because you don't know what is going to come in place of wildcard(?).

But for the second method printList2(List<T> list), the list is not immutable. You can add new elements to the list of Type T. Like T t = null; list.add(t).

 
vincent Lepore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello SwapanB,
Thank you very much for your answer.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!