wood burning stoves 2.0*
The moose likes Java in General and the fly likes null as arg in mehods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "null as arg in mehods" Watch "null as arg in mehods" New topic
Author

null as arg in mehods

Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Hello there! I have a question concerning null...
Consider this codefragment:
public class T {
public static void main(String[] a) {
T t = new T();
t.a(null);
}
public void a(String s) { System.out.println("1");
public void a(Integer s) { System.out.println("2");
public void a(Object s) { System.out.println("3");
}

Are there any common rules to this code? The compiler complains that this code is ambiguous; both the method with arg String and the one with arg Integer could be called by main. Not the one with arg Object. If I remove the one with arg Integer, it compiles and the method with arg String is called. Rules, anyone?
Thanks!
Amit Agarwal
Ranch Hand

Joined: May 09, 2001
Posts: 92
Hi Kaspar,
i tried your code and then tried a variant shown below:


This time also the code compiled successfully and the output was "myString".
So, my inference is that the compiler decides this "ambigous" situation on the basis of inheritance and calls the method with the most derived class as the parameter. In your case, string was inherited from Object.
Regards,
Amit
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Yes. I thought about that too, but Integer is 'more' derived than String. String inherits directly from Object while Integer extends Number extends Object...
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I'm not sure if this question is about ambiguous Method calls because of the parameter conversion or about null conversions. So we will tackle both.
If the parameter that is passed to a method or constructor does not exactly match the signature of any of the methods of the class there are certain allowable conversions that will be tried.
Primitives will be checked to see if they can be widened according to the following path:

byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double

Object references will be checked to see if they can be widened according to this chart:
From any class type S to any class type T, provided that S is a subclass of T. (An important special case is that there is a widening conversion to the class type Object from any other class type.)
From any class type S to any interface type K, provided that S implements K.
From the null type to any class type, interface type, or array type.
From any interface type J to any interface type K, provided that J is a subinterface of K.
From any interface type to type Object.
From any array type to type Object.
From any array type to type Cloneable.
From any array type to type java.io.Serializable
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.

If the widening conversion allows the parameter to become more than ONE the acceptable conversions (as in your case) and the class has methods that match each of those cases, the call becomes ambiguous and the compiler complains.
Since null can be converted to ANY other class or interface type as well as arrays, any overloading of methods that with primitive parameters is going to cause a problem. In your case when you removed one of the methods then it was clear which method to call.
Notice however, it you create a variable of type String and set it to null, and then pass it as the parameter - even though the value it STILL null, now the compiler knows that you intend to call the String version of the overloaded method.


The actual RULEs for acceptable parameter conversions can be found in the JLS in:
5.3 Method Invocation Conversion

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#12687

[This message has been edited by Cindy Glass (edited June 18, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well the above post is SUPPOSED to say:
any overloading of methods that with NON-primitive parameters is going to cause a problem.
But the darn UBB software keeps eating half the post when I try to edit it soooooo. . . . .
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Thanks Cindy!
I might be pulling my own leg now, but I actually think that I've got the hang of it now. Thanks for your rapid and great reply!
/Kaspar
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

A-HAA!

I'm not crazy!
http://www.javaranch.com/ubb/Forum10/HTML/000595.html

Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
The only thing that I got from the JLS link above is "From the null type to any class type, interface type, or array type." which Cindy mentioned above anyways.
Even then, how do you explain the behavior mentioned in Kaspar's note above. It's ambiguous only with respect to methods that have the String or the Integer parameter. Why not the method with the Object parameter? Also, I am not sure why sub-class of String is choosen as opposed to String in Amit's note above.
AJ
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4445
    
    5

Aj,
Java chooses the most specific version of the method (See JLS �15.12.2.
If you have
class A {}
class B extends C {}
class C extends B {}
then calling an overloaded method with versions that take Object, A, B, and C with null will always call the version that takes C because C is the most specific. However, if you have
class A {}
class B extends A {}
class C extends A {}
then you will get a compiler error if you call an overloaded method with null because of B and C.

Junilu - [How to Ask Questions] [How to Answer Questions]
Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
Nice. Thanks for the explanation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null as arg in mehods
 
Similar Threads
why not method(Object)
javabeat generics question doubt
(MyClass) null
Method invocation with null
Using Reflection to call super class method