Hi,
I think it may be the reason for that String is the subclass of Object. And
when you invoke the overload method, the JVM will first match the more
specific Class whether it can receive the parameter.
So you pass a null object, and it can be interpreted to a String object.
But if you have defined another class for example Foo which extends object
that null can also be interpreted to, you will get a error of ambiguous
method.
add which like:
void method(Test str) {
System.out.println(str);
}
Try it yourself.
Hugs,
Eric Cai
On Tue, Aug 4, 2009 at 3:36 PM, Babu Rajendran <[email protected]>wrote:
> Hi All,
>
> Can somebody explain me this behaviour? The output of the code below gives
> me null. When I ran it through the Debugger I found that the String version
> of method is executed. But why isn't the object version not executed? How
> does the compiler decide this ?
>
> class Test
> {
> public static void main(String[] args)
> {
> Test test = new Test();
> test.method(null);
> }
> void method(String str)
> {
> System.out.println(str);
> }
> void method(Object obj)
> {
> System.out.println(obj);
> }
> }
>
> Regards,
> Babu
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---