• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass a Null as parameter?!

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the code below. Will be the result of attempting to compile and run the code below.
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);
}
}
Answers
1.The code does not compile.
2.The code compiles cleanly and shows "Object Version".
3.The code compiles cleanly and shows "String Version"
4.The code throws an Exception at Runtime.
Can someone please explain to me why the output is "String Version" / Answer 2
I am very confuse!!
 
Vinny Chun
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, the answer should be Answer 3.
 
Vinny Chun
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another similar question:
Read the code below. Will be the result of attempting to compile and run the code below.
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
1.The code does not compile.
2.The code compiles cleanly and shows "StringBuffer Version".
3.The code compiles cleanly and shows "String Version"
4.The code throws an Exception at Runtime.
Now, this time is Answer 1!! :-(
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you invoke overloaded methods with the null argument, the compiler resolves the method invocation by taking the method with the most specific argument type. In your first example, String is more specific than Object and that's why the method with the String argument is executed.
In your second example, there is no method being more specific than the other since String and StringBuffer are not related in any way, thus resulting in a compilation error.
You may want to read the following JLS section:
15.12.2.2 Choose the Most Specific Method
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic