This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generics method overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics method overloading" Watch "Generics method overloading" New topic
Author

Generics method overloading

ravisha andar
Ranch Hand

Joined: Feb 25, 2011
Posts: 55
How does the below code complie when return type does not matter for method overloading

void m1(List<String> str)
{

}

String m1(List<Integer> str)
{
return "";
}

Please help me understand this.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4094
    
  18

I think you are mistaken. This doesn't compile.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Stephan van Hulst wrote:I think you are mistaken. This doesn't compile.

Actually, it does on my machine.

ravisha andar wrote:How does the below code complie when return type does not matter for method overloading

What makes you think it wouldn't...or are you referring to method overriding?

List<String> and List<Integer> are not related, except in the fact that they are Lists, so the compiler happily accepts your two methods as different signatures.

Winston

[Edit] Actually, that last statement is wrong. The compiler plainly does take return type into account when determining signature, because if you change that first method to return a String, then it won't compile.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4094
    
  18

What the... I just compiled using void the first time, I missed the String return type. It actually makes a difference?

Is this an oversight in the compiler, or does the JLS actually have anything to say about return types in combination with type erasure? I can't imagine the designers intended this to be legal.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Stephan van Hulst wrote:What the... I just compiled using void the first time, I missed the String return type. It actually makes a difference?

Is this an oversight in the compiler, or does the JLS actually have anything to say about return types in combination with type erasure? I can't imagine the designers intended this to be legal.

I dunno, but I suspect it may have something to do with the fact that List<Integer> and List<String> can't possibly be the same type (although I understand that after erasure they are) - in fact, they're not even related as far as generics is concerned.

Fun stuff though, and I agree it's not entirely consistent. Just can't be fagged to dredge through the JLS on a Sunday....

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

That's gotta be a bug.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20131
    
  30

Not necessarily. The method signatures of the two in native code are m1(Ljava.util.List;)V and m1(Ljava.util.List;)Ljava.lang.String; - different enough for the JVM to tell the two apart.

I have just tried it. The Java 6 compiler allows this code, the Java 7 compiler doesn't. Apparently it was a bug that's been fixed in Java 7.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:Not necessarily. The method signatures of the two in native code are m1(Ljava.util.List;)V and m1(Ljava.util.List;)Ljava.lang.String; - different enough for the JVM to tell the two apart.


Except that return type is not part of a method's signature. I'm gonna go with bug in the compiler.

I have just tried it. The Java 6 compiler allows this code, the Java 7 compiler doesn't. Apparently it was a bug that's been fixed in Java 7.


My IntelliJ also complained about it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8661
    
  23

Rob Spoor wrote:Not necessarily. The method signatures of the two in native code are m1(Ljava.util.List;)V and m1(Ljava.util.List;)Ljava.lang.String; - different enough for the JVM to tell the two apart.

I guess the question is - does the compiler actually use that native code sig to determine whether there's an error? In v6 clearly not; in v7, maybe it does.

Personallly, I quite like the v6 interpretation for applicability; obviously not for consistency though.

Winston
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20131
    
  30

Jeff Verdegan wrote:
Rob Spoor wrote:Not necessarily. The method signatures of the two in native code are m1(Ljava.util.List;)V and m1(Ljava.util.List;)Ljava.lang.String; - different enough for the JVM to tell the two apart.


Except that return type is not part of a method's signature.

It is in JNI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics method overloading
 
It's not a secret anymore!