• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Method Call

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain to me, why the program call results in printing "My String" to the output?

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ashok. But, is this for String vs object or there a general rul?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic