• 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

How does Type Inference (type parameter resolution) work?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading this book by Khalid Mughal for my SCJP and it has this particular example which i fail to understand.
Basically, there are two subclasses each for two different superclasses and they are compared using generic methods. One of the superclasses implements the Comparable interface, while the other doesn't.

I edited the unneccesary code (as much as i could )
Here goes the code:


jProgCMP = max(jProgCMP, jProgCMP); // (4) Compile-time Error
progCMP = WhoIsComparable.<ProgrammerCMP>max(jProgCMP, jProgCMP);

Why does the compiler fail to infer that the parameter type is a ProgrammerCMP when one isnt mentioned and both the objects are of the same subclass

progCMP = max(jProgCMP, cProgCMP);//Compiles and Runs
And likewise, how does the compiler correctly infer the parameter in the absence of an explicit type parameter, when the objects are of different sublasses.

Does the type of the reference being assigned a value play a role?
What are the exact steps that the compiler takes to infer a type parameter?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic