aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Call" Watch "Method Call" New topic
Author

Method Call

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Can someone explain to me, why the program call results in printing "My String" to the output?

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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 ]

Tony Morris
Java Q&A (FAQ, Trivia)
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
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

Joined: Mar 04, 2005
Posts: 91
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

Joined: Dec 23, 2004
Posts: 39
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

Joined: Feb 28, 2005
Posts: 30
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

Joined: Mar 04, 2005
Posts: 91
Thanks ashok. But, is this for String vs object or there a general rul?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
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
 
Don't get me started about those stupid light bulbs.
 
subject: Method Call