File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Call

 
M Rama
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me, why the program call results in printing "My String" to the output?

 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to your questions lies in Java Language Specification 15.12.2.2
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#18428
[ April 09, 2005: Message edited by: Tony Morris ]
 
M Rama
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Toni.

I understand that the method chosen is the most specific one. However, my question is, when passed null, why is String argument more specific than the Object?
[ April 10, 2005: Message edited by: M Rama ]
 
M Rama
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found another problem along the same lines, which looks like this:




which does result in a compiler error, as expected.
 
saran sadaiyappan
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yah this gives u a error as both string and stringbuffer will have the default value as null .Pass a string and u ll see the string method getting called.
 
ashok kumar
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rama,

string and stringbuffer r childs of object and both r in same level so this shows an error.But when we compare it with string and object string takes the preference over object.
 
M Rama
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ashok. But, is this for String vs object or there a general rul?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the missing piece in this discussion is that a null reference can be converted to any type of object. Therefore, calling method(null) could potentially invoke either method(String s) or method(Object o).

In general, a method declaration is "more specific" than another if its respective arguments could be converted (via method invocation conversion) to those of the other method.

In this case, a String can be converted to Object, but an Object cannot be converted to String, so the method taking the String argument is more specific.

However, a String cannot be converted to a StringBuffer, nor can a StringBuffer can be converted to a String (since neither extends the other), so an error results.
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

Have this too in your mind

adding small thing in a code

public class Test3
{
//added code
public void MyObject(char[] ch)
{
System.out.println("My Object");
}
//

public void MyObject(String s)
{
System.out.println("My String");
}
public static void main(String[] args)
{
Test3 t = new Test3();
t.MyObject(null);
}
}

This too result in ambiguous error
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic