This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About null

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alex Ku
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic