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

need clarification on this code !

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !
pl help me to know how the following code works
class test{
public static void main{
public void method(Object e) {
System.out.println("object");
}
public void method(String e) {
System.out.println("String");
{
public static void mani(String args[]) {
test a = new test();
a.method(null);
}
}
The output is "string " . pl explain.
thanks.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I think a correction is required in the second line of the code.
You can pass null to any object type. But when a String and Object gets null, it seems String can gets priority. If a StringBuffer and String are passed null, that is, if you change the arg to String and StringBuffer instead of String and Object, compiler gets confused and gives an error.
I noted this, but can someone, please explain, the real behaviour behind this.....
Thanks
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sivaram--
This is a slightly tricky one. Here, null can be an Object reference to either the java.lang.Object class or to the java.lang.String class. But java.lang.String extends java.lang.Object, so the JVM assumes that null is used as a reference for the String object. Hence the overloaded method with the String argument is called.
Art
[This message has been edited by Art Metzer (edited April 24, 2001).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This behaviour is in accordance with the rules. It just happens that the String method is "maximally specific" of all the overloaded methods and hence it gets invoked. You might want to checkout -
JLS Section 15.12.2.2 Choose the Most Specific Method

On a totally unrelated note, please post the code that compiles!!!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
[This message has been edited by Ajith Kallambella (edited April 24, 2001).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checkout the following program. Here there is no maximally specific method and hence the compiler errors out as expected -


[This message has been edited by Ajith Kallambella (edited April 24, 2001).]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler errors out? Worked fine for me
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS! I forgot to change Object to StringBuffer . Try it now !
 
sivaram subram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry ! that that second line in my code should n't be there .
but stil i am not able to get the funda.
thanks
sivaram
 
reply
    Bookmark Topic Watch Topic
  • New Topic