Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

varargs - method resolution principle???

 
Biju Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain the resolution rule, based on the program below...

---- Output -----------------

---------------------------------------------------
At lines marked 1 and 2 , I created object of B and assigned it in variables of type B and A respectively.
At lines 3 and 7, call is made on an actual instance of B, produce different results why so?
Similarly line 5 and 9, is giving different result.
Line 6 is giving a compile time error though the same call at line 10 is working fine..
Similarly line 8 is throws compile time error, where as the same call works at line 4

Someone please explain me why the calls gets routed to different methods when only the
type of the variable holding actual reference is different.?
[ July 26, 2005: Message edited by: Barry Gaunt ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, i was jsut reading on this.

1. Make sure old code does not break, so therefore, when you have these choices, this is the order

1. Exact match.
1B. Method with a Parent Class as the parameter.
2. widening of primitives.
3. Autoboxing and Unboxing
4. Varargs

Hope that helps, and that I got it right.

Mark
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun gives some advice:
Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Biju,

Which specific method to select, and under what situation, ambiguity arises are stated in JLS3 15.12.12 Compile-Time Step2: Determine Method Signature.

At lines 3 and 7, call is made on an actual instance of B, produce different results why so?

Hmmm...I was wondering how did you manage to get line 7 compiled without error?

Joyce
[ July 26, 2005: Message edited by: Joyce Lee ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, "which specification are you following?". Given that this part of the specification has jumped about more times than I've consumed beers (ok, maybe an exaggeration), the answer is flaky at best.

Here are related articles:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6249122
http://forum.jtiger.org/posts/list/24.page

You'll note that not even Sun know what they are doing with the specification in this regard (have you attempted to decipher the existing 3rd edition spec. yet?). You can bet (hope?) that Sun aren't going to put a question on the SCJP exam, that they clearly cannot answer themselves.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biju, which compiler's version "javac -version" are you using?

[Tony]: Given that this part of the specification has jumped about more times than I've consumed beers (ok, maybe an exaggeration), the answer is flaky at best.

Hey, how often do you drink beers?

Here are related articles:

Thanks for the articles, Tony.
[ July 26, 2005: Message edited by: Joyce Lee ]
 
Biju Joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tony, Mark , Barry, and Joyce....
Joyce, I use JDK version 1.5.0_04. I am using Eclipse 3.1.0 (Build id: 200412162000)IDE.
As you pointed out, Line 7 and 9 throw compile time error, when used the javac at command line. But it�s compiling well in Eclipse, which in turn points to the same JRE.
Hope these kind of confusing questions wont be asked in exam�
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Biju]:As you pointed out, Line 7 and 9 throw compile time error, when used the javac at command line. But it�s compiling well in Eclipse, which in turn points to the same JRE.

The compiler used by Eclipse is not the same as the Sun JDK's compiler. My suggestion is to use Sun JDK's compiler when experimenting with the code for SCJP.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic