This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which method to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which method to use?" Watch "which method to use?" New topic

which method to use?

Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
from (with modifications)

I thought this would be a compile error, 'coz method(null) would be ambiguous (it wouldn't know which method() will be invoked.) But it compiles and prints out "Integer Version" when run! Also, when method(String) is uncommented out, then the compiler complains about ambiguous methods.
Tybon Wu
Ranch Hand

Joined: Jun 18, 2002
Posts: 84
Note the inheritance tree:
Since null is compatible to any object, the one furthest down the compatible path will be chosen, in this case, Integer.
If you also have the overloaded method with the String parameter, then that will create two compatible paths for null, which causes the ambiguous methods error.
[ June 19, 2002: Message edited by: Tybon Wu ]

It is sorta covered in the JavaRanch Style Guide.
subject: which method to use?