• 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

null argument to a method - what is being passed??

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question: what is the output?
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Version");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
What is actually being passed in when you call the method with "null" as the argument?
Thanks
Sharda
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code won't compile because it's ambiguous.
 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here's a similar question that I came across:
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);
}
}
What is being passed through the "null" and which method does it invoke?
Thanks
Sharda
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to answer such questions is to compile and run it yourself.
In this case, the String method is called because it is more specific than the Object method.
In the original code, neither the String method nor the StringBuffer method was more specific, so the compiler complained.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This time it is not ambiguous. String isa Object, so
on the principle that the compiler always chooses the method with the most-specific type of formal parameter, the String taking method will be chosen.
-Barry
PS I compiled it and ran it to verify that - did you?
PPS Somehow I new Ron would get in first
[ September 17, 2002: Message edited by: Barry Gaunt ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first guess was it should be ambiguous as well, but apparently the "lowest" class in the hierarchy is preferred. See the code below:

The output is "B version". I'll have to check the specs on this...
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never mind
 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone. I tried it but couldn't understand the results. Hence the post.
Sharda
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I am wondering about why no Exception was thrown at the run time. See code below:

In Java API, NullPointerException should be


Thrown when an application attempts to use null in a case where an object is required.


I guess it depends on whether the method access and modify the object, but I am not sure. Could anybody give me some helps?
Thanks!!
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get a NullPointerException if your object reference is null, AND you use this object reference to invoke a method.
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anthony, Thanks a lot for pointing out this!!
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharda, Consider the following class heirarchy....

Level1_A and Level1_B can be considered as sibling classes.
Case 1)
If you have methods as follows,
void someMethod( Base obj ); // 1
void someMethod( Level1_A obj ); // 2
void someMethod( Level2_A obj ); // 3
and if you invoke someMethod() with argument null, then obviously the most specific method would be someMethod(Level2_A) - because any parameter that can be passed to method 3 can be passed to methods 1 & 2 without the need for an explicit cast.
Case 2)
If you have methods as follows,
void someMethod( Base obj ); // 1
void someMethod( Level1_A obj ); // 2
void someMethod( Level1_B obj ); // 3
and you invoke someMethod() with argument null, then you have ambiguity because there is no 'most specific' method - both are at the same level and the condition for most specific method fails. The parent (Base) cannot have a favourite child (Level1_A or Level1_B).
Case 3)
If you have methods as follows,
void someMethod( Base obj ); // 1
void someMethod( Level1_A obj ); // 2
void someMethod( String obj ); // 3
and you invoke someMethod() with argument null, then you have ambiguity - just as in Case 2, They can be considered siblings.
Case 4)
If you have methods as follows,
void someMethod( Base obj ); // 1
void someMethod( Level1_A obj ); // 2
void someMethod( Level1_B obj ); // 3
void someMethod( Level2_A obj ); // 4
and you invoke someMethod() with argument null, then you STILL have ambiguity because now the confusion is between invoking the Level1 method or the Level2 method i.e., between 3 & 4.
Hope this clarifies your doubts.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll also get a NullPointerException if your object reference is null, and you try to use it to access a field.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic