• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

String or Object

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred is correct.

Output is: Byte.
Inheritance hierarchy is: Object -> Number -> Byte.
-Peter
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic