aspose file tools*
The moose likes Beginning Java and the fly likes same erasure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "same erasure" Watch "same erasure" New topic
Author

same erasure

Peter Kirk
Greenhorn

Joined: Oct 12, 2011
Posts: 12
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
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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

Joined: Oct 12, 2011
Posts: 12
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

Joined: Jul 18, 2011
Posts: 287
Need more proof?

try the following code
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4364
    
    8

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Kirk
Greenhorn

Joined: Oct 12, 2011
Posts: 12
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

Backwards compatibility is indeed the (main) reason for type erasure, and many Java programmer hates it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: same erasure