The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem in Core Java

1 reply on 1 page. Most recent reply: Sep 2, 2002 12:19 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
siddhartha

Posts: 15
Nickname: saddysan
Registered: Jul, 2002

Problem in Core Java Posted: Sep 1, 2002 11:21 AM
Reply to this message Reply
Advertisement
Here is a small program:

public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Version");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Ourput is:
String Version

my doubt is why ?

saddy


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Problem in Core Java Posted: Sep 2, 2002 12:19 PM
Reply to this message Reply
It is because Java finds two methods matching null argument:
1. IT is String arg
2. It is Object arg

Now Java will choose which one is more specific and it finds that String argument method is more specific and that is why it calls that methods always. To find that which method is more specific, you can try to call other method from within the body of other one and see if it is possible. Object is not always a String, however a String is always an Object. So String version of method argument is more specific. Also, refer to JLS for more details on this topic. Here, null can be assigned to String as well as Object. However, String can also be assigned to Object and not vice versa.

Thanks
Kishori

Flat View: This topic has 1 reply on 1 page
Topic: Compositon  and Relationship Previous Topic   Next Topic Topic: applets-package-images-appletviewer problem !!?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use