aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing null to the method arguement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing null to the method arguement" Watch "Passing null to the method arguement" New topic
Author

Passing null to the method arguement

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,

Please go thru 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);
}
}

Output : String Version

We are actually passing null to the method call, but how is it guaranteed that only the method(String) is called. Can't null be sent as parameter for method(Object).
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Strings null by default - the closets match
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Why does the following code not even compile, giving me a compile-time error that null is ambiguous?



Kaydell
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

For answers to both questions (Chandra's and Kaydell's), see SCJP FAQ - What is a most-specific method?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

This is because during compilation, the compiler actually takes a decision of choosing the appropriate version of the method. The decision is taken in such a way that, the chosen method is * very specific * so that the * less specific * or * generic * method can in turn be invoked by within the chosen method (delegation can happen).

In case of the OP's question, java.lang.String is chosen because the method invocation handled by the String object can very well be passed on to the next version which handles java.lang.Object without any compile-time error. That's why the most specific method of (java.lang.String) version is chosen and invoked at the runtime.

In case of primitives arguments in the methods, the primitive type which can be implicitly converted to the other type is chosen, so that the call can be delegated to the higher datatype version without any compile time errors. For example, in case of int and long, int version is chosen.

For more info on this, you may please read Danchisolm's Chapter 12 which deals with this Overloading questions in a good manner.

Hope this clears.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Kaydell,

Since because java.lang.String and java.lang.StringBuffer both in the same hierarchy (direct descendants of java.lang.Object) both are eligible for a call. Thats why the compiler is unable to choose the most appropriate and specific method.

Hope this helps!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks to all of you for such a nice explanation. But I'm extending my query here .

From the link provided by Marc
"For answers to both questions (Chandra's and Kaydell's), see SCJP FAQ - What is a most-specific method?" there is a statement
"If it's not possible to identify a most-specific method from among the applicable methods, then the invocation is considered "ambiguous" and results in a compile-time error.
Note, however, that a null reference could apply to any object type. Therefore, the call method(null) would also invoke the most specific method -- in this case, method(Cat cat). ",
now from our example how do we determine that the call method(null) would invoke method(String) rather than method(Object). Should I understand that since String is more specific when compared to Object, method call having String as arguement is invoked.

Hope I framed the sentence correctly and meaningfully.

Thanks
Ravi
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
To find out which is more specific, we have to understand the hierarchy of the class. In your case, Heirarchy is like this:
Object comes first, then String class or in in other words we can say that, String extends Object.
Object
|
String


SO as we down the hierarchy, the class will become more specific and as we go up, the class will become more general.


Hope this will clear your doubt


Gunjan Kumar
SCJP 1.5
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Chandra,


Should I understand that since String is more specific when compared to Object, method call having String as arguement is invoked.


Yes. You are right. As Gunjan Kumar had also pointed out, the more you come down in the hierarchy, you will meet up with the more specific objects.

One more way to approach is also: The super class (generic) object knows how to deal with the sub (specific) class objects. Thats why the specific method is allowed to be invoked. But the reverse case is not true.

HtH.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Thanks for the help everyone.

It seems like that if I want to pass null as an argument that I should make it have a type by making it a constant:

public static final JFrame NO_PARENT_WINDOW = null; // if this makes sense.

And for arrays, the book "Effective Java" suggests that we return zero-length arrays rather than returning null as a method result so that no special-casing needs to be involved anywhere along the way. And a zero-length array is as immutable as null, and a zero-length array doesn't take up manyresources since you can construct just one that is a constant.

public static final Animal[] NO_ANIMALS = new Animal[0];

Kaydell
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Originally posted by Kaydell Leavitt:

It seems like that if I want to pass null as an argument that I should make it have a type by making it a constant:

public static final JFrame NO_PARENT_WINDOW = null; // if this makes sense.


You don't need to use a constant. The following code will compile without problems:



SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Kaydell Leavitt:
...It seems like that if I want to pass null as an argument that I should make it have a type by making it a constant...

There's no need to define null as a constant, because it's already built into the language as a literal (like true and false).

The "null" literal denotes a reference with a special value of all zeros instead of a memory address for an object. This special value can be assigned to a variable of any non-primitive type, which is what happens when you pass null as a method argument.

So assigning null to a final variable simply to pass it into a method is an unnecessary step. Use the literal instead.
[ June 20, 2007: Message edited by: marc weber ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

As marc weber said, "null" is in turn a literal added into the Java Language. So why don't you simply make use of it?


Originally posted by Kaydell:
And for arrays, the book "Effective Java" suggests that we return zero-length arrays rather than returning null as a method result so that no special-casing needs to be involved anywhere along the way. And a zero-length array is as immutable as null, and a zero-length array doesn't take up many resources since you can construct just one that is a constant.


I think the statement holds good as the need for checking "not null" before proceeding is avoided here. But it depends on the programmer's choice !
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally posted by Marc Weber:
The "null" literal denotes a reference with a special value of all zeros instead of a memory address for an object. This special value can be assigned to a variable of any non-primitive type, which is what happens when you pass null as a method argument.


Thanks for the additional and useful information about "null" marc!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Its been quite some time I came back to my post. Thanks to all for clarifying my doubt.
 
 
subject: Passing null to the method arguement