• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic doubt on return type using wildCard

 
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused about the following code, please help



This does not compile. As we cannot cast <? extends CharSequence> to <String> (Given String implements CharSequence)

Code fixes with following change:



Please elaborate on this. I though "? extends CharSequence" should work for String, Is there any issue related to ambiguity of return type??..

Source : K&B self test questions.
 
author
Posts: 23951
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

AmanZeeK Verma wrote:
Please elaborate on this. I though "? extends CharSequence" should work for String, Is there any issue related to ambiguity of return type??..



The wildcard "? extends CharSequence" does *not* mean any type that IS-A CharSequence. It means, that it is an unknown type, and the only thing that is known about it is that it IS-A CharSequence. The compiler can't confirm that a Collection<String> is the correct item to return. The compiler can't confirm that the value returned is a Collection<String>, and hence, can't do the assignment of the returned value.

Henry

 
AmanZeeK Verma
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, thanks for the response.

Based on above,
Can we generalize a statement that return type of method cannot use wildCard (?)
 
Henry Wong
author
Posts: 23951
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

AmanZeeK Verma wrote:
Can we generalize a statement that return type of method cannot use wildCard (?)



Oops. Sorry. I worded the answer incorrectly. Just fixed it.

Henry
 
AmanZeeK Verma
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
updated the code posted.

Thanks for the help.

Conclusion:
Collection<E> to be returned sounds logical and works fine.


I still have a doubt about generalizing the statement --- Can we under any condition return a "? extends Something" from a method. -- I don't think so, if we can return please help with a sample method or condition.








 
Henry Wong
author
Posts: 23951
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

AmanZeeK Verma wrote:
I still have a doubt about generalizing the statement --- Can we under any condition return a "? extends Something" from a method. -- I don't think so, if we can return please help with a sample method or condition.



Sure. It is perfectly fine to return a wildcard. It just has to be used as a wildcard. For example, it would have been fine to call the code like this...



Henry
 
AmanZeeK Verma
Greenhorn
Posts: 29
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

perfectly fine to return a wildcard. It just has to be used as a wildcard





Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic