Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generics - nested type parameters

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

What about this:


We know why it is not allowed to do that. However

is ok. Is the sole reason that the compiler makes sure that nobody adds
anything into the list ?


Thanks,

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

have a look at What do multi-level wildcards mean? in the Java Generics FAQs.
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manfred,

Thanks for your reply.
Yes - I have read it already. And I find it a little bit
misleading. That's because

? extends Pair<String,?> is particularly pointing to a Pair type, not
the wildcard. It would work if not the erasure (I mean List<Pair<String,?>>
, although we would get a runtime error). But saying List<? extends ....>
we are safe, because it doesn't matter what we have there - we can't add
anything anyway ... Note, that if we had List<? extends Pair<String,String>>
then we wouldn't be able to stuff a List<Pair<String,Integer>>.
So the inside type parameter is binding regardless of whether the
parameterized type is a part of a bounded wildcard or not. If it is, then
the only reason why compiler allows it is the it knows you can't add
anything to it anyway. That's how I see it. But I might be wrong
I'll be happy to read what others think about it.

Cheers,

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

nobody can answer your question unless you give some information about class Pair.
Is it like this:
class Pair<T, S> {...}
or like that:
class Pair<T> {...}



Bu.
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Burkhard,

That's a good point

It's Pair<T,S>


Cheers,

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

I was just thinking - from the context of my previous
posts, you just couldn't have a Pair<T>. I'm using
two place holders everywhere I'm using a Pair, so it had
to be Pair<T,S> - not Pair<T>. Obviously it also couldn't
be DEFINED as Pair<T,T>.

It's just my thought


Cheers,

Adrian
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fwiw - i'm about 99% sure that this isn't on the real exam. I wish I was 100% sure, but still, I'm pretty darned sure

My advice would be to make sure you're solid on everything else, and then, if you still want to, you could come back and look into this topic.
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,

It's nice to be answered by the author of a book
which I find as one of the best books a have ever read

Yes - I thought so, but I really don't like to leave
some misunderstand concepts behind me :/

Anyway, I'm currently doing some preparation tests
and to be honest, I didn't expect it takes so long !
(assuming you are then reviewing all the questions to
see why you were right and why you were wrong ...)
So I'll be coming back here quite often


Cheers,

Adrian
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic