Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Are unbounded wildcards same as using the formal type parameter (T)?

 
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I continue studying generics from OCP study guide of Jeanne and Scott and I'd like to share with you a question about a definition of Unbounded wildcards.

In page 119 there's a code to explain that an unbounded wildcard allows to receive any type of object.

They say:
What we really need is a List of whatever. That's what List<?> is. The following code does what we expect:



So what I see is that there is no difference between this unbounded wildard (?) and the common use of generics. And here one similar example:



So i would like to see your opinion cause maybe there is something that i'm not having in mind.

Thanks!
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that in this case there is any difference between both. I would prefer with <T> because it looks cleaner, but that is personal preference.

One huge caveat: I am absolutely not an expert on generics.  But i'm sure that others will share their opinion as well.
 
Marshal
Posts: 65814
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two are different, but you happened to choose an example using only features showing no difference.
Because the first example declares a List<?> which is a List of Unknown type, it is not possible to add any objects to it (I think you can only add null), and anything coming out of it can only be regarded as type Object. In the second example, you are declaring the method as being generic, but you are not doing anything specific to the T. All you're doing is calling its String#valueOf() on each reference, and there is no need for any particular reference type.
I think you ought to be writing...but again you have chosen something which only uses features of the Object class, so you won't notice any difference.
 
Pablo Napoli
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both guys but the Ritchie's post was the crux of the matter for me when

it says: List<?> which is a List of Unknown type, it is not possible to add any objects to it

That was very clarifier for me because it was something that I wasn't understood at all.
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is why the book says they are "logically immutable". It took me a while to figure out what that meant.
Screenshot-2019-09-29-at-21.36.08.png
[Thumbnail for Screenshot-2019-09-29-at-21.36.08.png]
 
Brecht Geeraerts
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this thread from a few weeks ago can be of interest to you?
 
Pablo Napoli
Ranch Hand
Posts: 62
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brechts that's the same as I'm pointing out now. I think the matter comes down to wildcards are useful to be used as input parameters so that they can receive an object of some type. Since the following statement works but makes no sense: List<?> list = new ArrayList<Integer>(). And why does it make no sense? Because we cannot add anything to the list. But the code bellow (inspired on the OCP study guide) is different because we can pass to the method any List that uses any of the implementations of Number. And if we had had "public void total(List<Number> list){" instead of line 4 it wasn't gonna compile because List<Integer> cannot be converted to List<Number>.



This is my own conclusion based on all the information that you guys gave me so correct me please if I'm wrong.
 
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a newly declared type parameter any time you would have used a wildcard. But why would you declare a parameter if you're not going to use it? Besides, if your method takes more than one parameter with a wildcard, you would have to declare a new type parameter for each one of them.

In this example, the type parameters T and U are unused. We don't return a generic type that uses T or U, nor do the firstList or secondList's type parameters relate to each other. That means we can leave the type parameters away and use wildcards instead. I think this is much tidier:

Wildcards also allow you to use the same variable to hold generic types with different type arguments. For instance, if you need to keep track of which of two lists is the smaller and which is the larger, you can do the following:

This would not have been possible if you couldn't use wildcards to declare smallList and largeList.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!