This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

null as parameter  RSS feed

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Can anyone explain why the output is "String version"?
Thanks
kanchan
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule stated in JLS 15.12.2.2 is: Choose the Most Specific Method. So in your case, String is extends from Object, passed 'null' is more close to String than Object. Similarly, if your method para. are Integer and Number, Integer is choosed, but if you have String and Integer, it will be compile error because neither String nor Integer is more specific to each other.
 
Sheriff
Posts: 12339
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting...
Did a few experiments and it seems that Java will always go for the bottom-most class in a hierarchy (Object <-- String), (A <-- B <-- C). It complains about ambiguity when the parameters are not directly related.
<pre>
class A {}
class B extends A {}
class C extends B {}
class WhichOne
{
public void method(A o)
{
System.out.println("A version");
}
public void method(B o)
{
System.out.println("Bersion");
}
public void method(C o)
{
System.out.println("C version");
}
}
WhichOne x = new WhichOne();
x.method(null); // prints out "C version"
</pre>

Haven't found anything in the JLS to suggest this though... will keep looking... Oh, looks like Haining Mu found it.

[This message has been edited by JUNILU LACAR (edited June 07, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!