• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

About null

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic