String or Object
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
O \ P of foll code
public class xxx
{
void ccc(Object obj)
{
System.out.println("In Object ");
}
void ccc(String str)
{
System.out.println("In String");
}
public static void main(String [] argv)
{
xxx x = new xxx();
x.ccc(null);
}
}
[This message has been edited by gunjan kuwadia (edited February 16, 2001).]
public class xxx
{
void ccc(Object obj)
{
System.out.println("In Object ");
}
void ccc(String str)
{
System.out.println("In String");
}
public static void main(String [] argv)
{
xxx x = new xxx();
x.ccc(null);
}
}
[This message has been edited by gunjan kuwadia (edited February 16, 2001).]
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
According to the JVM specifications the null literal can be converted to any reference type. Therefore, in your example we can assume that the null reference type will be converted into the lowest level class found in your method parameters. Since the lowest level class found in your method parameters is String (subclass of Object, therefore lower level), it will call that method. It is interesting to note that any other method that has only one non-primitive parameter will cause a compiler error. For example:
method(String s) and
method(StringBuffer sb)
or
method( Integer i) and
method( Double d)
Regards,
Manfred.
According to the JVM specifications the null literal can be converted to any reference type. Therefore, in your example we can assume that the null reference type will be converted into the lowest level class found in your method parameters. Since the lowest level class found in your method parameters is String (subclass of Object, therefore lower level), it will call that method. It is interesting to note that any other method that has only one non-primitive parameter will cause a compiler error. For example:
method(String s) and
method(StringBuffer sb)
or
method( Integer i) and
method( Double d)
Regards,
Manfred.
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Manfred is correct.
Output is: Byte.
Inheritance hierarchy is: Object -> Number -> Byte.
-Peter
Output is: Byte.
Inheritance hierarchy is: Object -> Number -> Byte.
-Peter
posted 24 years ago
Hi Manfred,
Will you explain me in detail "It is interesting to note that any other method that has only one non-primitive parameter will cause a compiler error. "
I am a bit confused.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Manfred Leonhardt:
Hi,
According to the JVM specifications the null literal can be converted to any reference type. Therefore, in your example we can assume that the null reference type will be converted into the lowest level class found in your method parameters. Since the lowest level class found in your method parameters is String (subclass of Object, therefore lower level), it will call that method. It is interesting to note that any other method that has only one non-primitive parameter will cause a compiler error. For example:
method(String s) and
method(StringBuffer sb)
or
method( Integer i) and
method( Double d)
Regards,
Manfred.
Hi Manfred,
Will you explain me in detail "It is interesting to note that any other method that has only one non-primitive parameter will cause a compiler error. "
I am a bit confused.
Peter Tran
Bartender
Posts: 783
posted 24 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Deepak,
This is much easier to explain with an example. See my earlier example:
If I add another overloaded method that excepts a class type that is a subclass of Number (e.g. Integer, Long, Boolean, etc.), the compiler will throw an exception because it doesn't know which one to call. The overloaded version for Byte already exist. You can't have the overloaded methods accepting a non-primitive type that are at the same level in the inheritance hierarchy. NOTE this only applies to the lowest level of the inheritance hierarchy. E.g. the following code will not compile.
The compiler error is:
WhichOne.java:22: Reference to aMethod is ambiguous. It is defined in void aMethod(java.lang.Byte) and void aMethod(java.lang.Integer). yikes.aMethod(null);
-Peter
This is much easier to explain with an example. See my earlier example:
If I add another overloaded method that excepts a class type that is a subclass of Number (e.g. Integer, Long, Boolean, etc.), the compiler will throw an exception because it doesn't know which one to call. The overloaded version for Byte already exist. You can't have the overloaded methods accepting a non-primitive type that are at the same level in the inheritance hierarchy. NOTE this only applies to the lowest level of the inheritance hierarchy. E.g. the following code will not compile.
The compiler error is:
WhichOne.java:22: Reference to aMethod is ambiguous. It is defined in void aMethod(java.lang.Byte) and void aMethod(java.lang.Integer). yikes.aMethod(null);
-Peter
| My, my, aren't you a big fella. Here, have a tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






