• 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

Generics - wildcard

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I feel like I understand generics and its purpose, particularly around upper and lower bounding, I get that.

However, I can't quite work out what  unbounded wildcards  ? are really doing... Is it simply, being translated by the compiler as Object?

Thus, why doesn't the below compile? I can't see the purpose of the unbounded wildcard (?).



Thanks as always
 
MyExamCloud Software Support
Posts: 753
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The wildcard character (?) can be used to specify an unbounded wildcard type, as shown in List<?>. This creates a list of unknown type, and can be useful in certain scenarios such as:

1. When writing a method that can be implemented using functionality provided in the Object class.
2. When using methods in a generic class that don't depend on the type parameter, such as List.size or List.clear.

One example of such a method is printList, which can print a list of any type. However, if it is specified as List<Object>, it will only be able to print lists of Object instances and not subtypes like List<Integer>, List<String>, List<Double>, etc. To make printList more generic, it can be rewritten using List<?> to accept any type of list.



Refer: https://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html
 
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Bant wrote:I feel like I understand generics and its purpose, particularly around upper and lower bounding, I get that.

However, I can't quite work out what  unbounded wildcards  ? are really doing... Is it simply, being translated by the compiler as Object?


No, better to say it's being translated as a List with lower bound of Object, a List<? extends Object>.  But it's still a wildcard.

Tim Bant wrote:Thus, why doesn't the below compile? I can't see the purpose of the unbounded wildcard (?).


Well to answer the easier part first, there is no purpose; it doesn't make any sense here.

But the reason it doesn't compile is, since aList is a List<?>, the compiler treats it as if it might be a List of anything - a List<Object> or List<String> or List<Integer> or List<SomeUnknownClass>.  So when you get to

that could make sense if it were a List<Object> or List<String>.  But not if it's a List<Integer> or List<SomeUnknownClass>.  Because the string "Hello" is not a subtype of Integer or SomeUnknownClass. So, the compiler can't allow that, and doesn't.  

There is, of course, no reason to declare the type as List<?> if you know you're going to want to add a String to it later.  You can't do that.  You might want to declare a List<?> for other reasons - Jhonson gave a good example of that.  But in that example, he didn't try to add anything to the list, in the code that declared it as List<?>.  Rather, the list was created outside that method, and passed in.  He didn't need to add anything, or use any method calls on the elements of the list, other than an implicit call to toString() which is guaranteed to exist for any object.
 
Tim Bant
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks both
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
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