aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing null as reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing null as reference" Watch "Passing null as reference" New topic
Author

Passing null as reference

venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
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??
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The most specific method is chosen.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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 ]

all events occur in real time
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
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

Joined: Apr 29, 2006
Posts: 106
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing null as reference