• 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

same erasure

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, if I try to write two methods like the following, I get an error: "Method cvt(List<Integer>) has the same erasure cvt(List<E>) as another method in type".



Isn't this sort of thing possible in Java? I would have thought that the types "List<String>" and "List<Integer>" were sufficiently different to allow method overloading.
Is the only solution to give the methods differernt names?

Thanks,
Peter
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not overloading the method here. Method Overloading mean same method name but different parameters. The return types can either be same or different.

Generics Type erasure. List<String> list and List<Integer> list are one and the same.
 
Peter Kirk
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsha Smith wrote:You are not overloading the method here. Method Overloading mean same method name but different parameters. The return types can either be same or different.

Generics Type erasure. List<String> list and List<Integer> list are one and the same.



I see, thanks - not quite the same as what I'm used to. I would have thought that "List<String>" and "List<Integer>" were different - therefore method overloading would be possible.
I'll just rename my methods then.
Seems a little odd though that Java seems to be telling me that "List<String>" and "List<Integer>" are the same here, but if I try the following, Java tells me they are not the same!
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need more proof?

try the following code
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Kirk wrote:Seems a little odd though that Java seems to be telling me that "List<String>" and "List<Integer>" are the same here, but if I try the following, Java tells me they are not the same!


In Java, because of certain compromises made when generics were introduced into the language, the generic information only exists at compile-time. At run-time, the information is lost. That's why those codes are considered the same - they have the same "erasure", meaning they have the same signature once the generic info is erased.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, at runtime all that remains is And those are certainly the same.
 
Peter Kirk
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:In other words, at runtime all that remains is And those are certainly the same.



Yes thanks, they are the same - and I have now come to understand a little more of Java's handling of generics. It appears that generics is not a runtime thing in Java, only compile time. The JVM has no idea of generic types, and the generics we use in code is only syntactic sugar. Apparently this is in part (or mostly?) due to a neccessity of maintaining backward compatibility. I was already aware what "method overloading" was - just not aware of Java's generic interpretation. I prefer the .net/c# idea, where something like my original method overloading would compile and run fine. But it's not a biggie - just a "surprise" when you don't know.

 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Backwards compatibility is indeed the (main) reason for type erasure, and many Java programmer hates it.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn’t the real reason that they ran out of time or money and then had to add generics eight years later? And erasure makes it a right mess.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic