Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About null " Watch "About null " New topic
Author

About null

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Dear all, I get this question from a java quiz.
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.
The answer is 1.
Can you help me why the answer is that?
thanks
daniel
Macon Pegram
Greenhorn

Joined: Mar 01, 2002
Posts: 12
That is because the null parameter is non-deterministic. null can be used in assignments to any type of object. Your overloaded methods each take one object parameter. It is impossible to determine if your intent is to pass the null to the method that takes the String or the StringBuffer. So your reference to the method is ambiguous.
If you had a method that took a primitive and another took an object however your code would compile (because only 1 signature is looking for an object, and null cannot be assigned to a primitive).
Try out this code:

This code will print "String Version" when run.
[ March 05, 2002: Message edited by: Macon Pegram ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
When the compiler has to resolve a method invocation expression (here question.method(null)) and determine whic method is to be invoked, it will take the most specific one according to the method's name, the type and order of the arguments. In this case, we have to most specific methods since String and StringBuffer are not related in any way (they are siblings), the compiler just doesn't know which one to choose. The null reference could apply to the first method as well as to the second one, and there is no deterministic way to know which one to take. So the compiler complains !


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Just as a follow-up to make you think about this a little more, Fisher, what would be printed, if anything, if you changed main to look like this?

Corey


SCJP Tipline, etc.
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Hi,
I just finished one mock exam.
There is one question similar to this but...
Here is the code:

kawaii
[ March 05, 2002: Message edited by: kawaii desu ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by kawaii desu:
Hi,
I just finished one mock exam.
There is one question similar to this but...
Here is the code:

kawaii
[ March 05, 2002: Message edited by: kawaii desu ]

Is there a question here? Or just a statement?
Corey
swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
In the code given be Kawaii..the call
question.method(s); never gets executed as
the the expresion in if results false..as s is null.
But i have this code which doesnot generate any compiler error.....i couldnt understand why?
code:
class A
{
public void method(Object o){System.out.println("object");}
public void method(String o){System.out.println("string");}
public static void main(String ar[])
{
new A().method(null);
}
}
this results in---string to be printed.

i couldnt get why the compiler doesnot raise error
of ambiguity as all Objects can take null ???..please help.
thanks
swapna


SCPJ2
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
In this case, the two methods take an object paramater that are related. A String IS and Object.

Since String inherits from Object, the compiler uses the rule of "most specific" when using your call with a null value. There are only two possible methods it can invoke, and it chooses the String method, because it's most specific. If you had two unrelated parameters, then the compiler would not be able to guess which one you wanted. But here, since a String is an Object, it could use either method without ambiguity. But it chooses the String.


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About null
 
Similar Threads
Mock Exam Questions (null)
Use of null in method call
Please Explain
Doubt in overloaded method related question
Pass a Null as parameter?!