• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic methods - question on type erasure

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I wonder if anyone could help me understand something ...

I get that (because of type erasure), the compiler treats x here as being of type Object NOT of type Integer (even though when myMethod() was called at line 6, it was passed an int (which gets boxed to an Integer). So it seems that the compiler treats x as an Object, and will allow x to be used (inside myMethod) for calls to methods of Object class (like toString() and getClass - lines 11 and 12) BUT cannot be used for calls to methods of any other class (e.g. Integer - which is the actual class of the x variable).

My question is : It that ALWAYS the case, is x only ever usable for methods of Object class and if so does this mean that generic methods aren't that useful ???


 
author
Posts: 23928
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

John Mulholland wrote:
I get that (because of type erasure), the compiler treats x here as being of type Object NOT of type Integer (even though when myMethod() was called at line 6, it was passed an int (which gets boxed to an Integer). So it seems that the compiler treats x as an Object, and will allow x to be used (inside myMethod) for calls to methods of Object class (like toString() and getClass - lines 11 and 12) BUT cannot be used for calls to methods of any other class (e.g. Integer - which is the actual class of the x variable).



The reason for the error has nothing to do with type erasure. The exact type is simply not available at that line. The compiler can't know how the method is going to be used -- after all, what if a different class uses it differently.

John Mulholland wrote:
My question is : It that ALWAYS the case, is x only ever usable for methods of Object class and if so does this mean that generic methods aren't that useful ???



No. It is not always the case. You can make it a bounded type generic, in which case, it can assume an IS-A of the type.

Henry
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry - that makes sense to me now.

I need to read up some more about type erasure to get a better understanding of exactly what it is (and is not).

Regards, John
 
You can't expect to wield supreme executive power just because
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic