• 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

Passing null as reference

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class D
{
void met(Object ob)
{
System.out.println("Object-"+ob);
}

void met(String s)
{
System.out.println("String-"+s);
}

public static void main(String[] args)
{
new D().met(null);
}
}

met() method is overloaded,it either takes Object or String as an argument.But when we are sending null as an argument why does met() method with String argument gets executed??why dont we get nullPointerException??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most specific method is chosen.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When overloaded, the most specialized (most extended) type will be used. As String is more specialized than Object, the String method will be called.
This works also for primitive values.
For methods it is generally ok to call them with a null.
Therefore you'll often see methods, that (in their body) check if a parameter == null.


Second, you get a null pointer exception only, when you try to access one of the fields of a reference (with the dot operator) that points to null. Something like e.g. reference.hashCode();
The methods here don't do that. They just print it.
So no null pointer exception.

Yours,
Bu.
[ November 16, 2006: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you print don't you access the toString() method?
So isn't null.toString being called which should result in a NullPointerException?

I know this doesn't happen in reality, but it seems like null is being special cased in order to prevent this happening in the case of printing. For example in C you won't print out null just because you passed in a null pointer. This is something that has bothered me since I first came across it in Java and I would like someone to explain how this works?
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got your point Bukhard, I think String is a subclass of Object thats why met() methods with String parameter was called.
I also understood when nullPointerException gets thrown.Thanks for help.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by F Fountain:
When you print don't you access the toString() method?
So isn't null.toString being called which should result in a NullPointerException?

I know this doesn't happen in reality, but it seems like null is being special cased in order to prevent this happening in the case of printing. For example in C you won't print out null just because you passed in a null pointer. This is something that has bothered me since I first came across it in Java and I would like someone to explain how this works?



Take a look at the API for String.valueOf(Object), you will see that null is handled specially.
 
Franz Fountain
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me see if I understand what's going on.

So you're saying that the println() method will take any Objects that are passed in and run the static String.valueOf() method on them. This method will return a String. Usually the String it returns is the result of the toString() method of the Object, but in the special case of a null Object the valueOf() method will return "null". That is the String "null".

I think my confusion was that I thought that the toString() method was run on an Object directly, but actually it is a 2 step process. I think I had this confusion because most beginner Java books will simplify the situation and say that println() prints out the result of toString() on an Object. I think even K&B says this somewhere, but I can't say where. I guess this is just another of those instances where you "lie" to the beginners so they won't get too scared or confused.

Thanks for the clarification. This is a great site for learning about Java!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic