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

Regarding String

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First consider the below example :
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Output: The code compiles cleanly and shows "String Version"
Now consider the below example :

public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Output : The code does not compile.
Why is it so ?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
From you first example, you know that null is handled by the JVM as a string not an object.
The best answer that I can come up with for your second example is that the only difference between String and StringBuffer is that String is immutable and StringBuffer is mutable. Since that is the only difference the compiler can't tell which method to call because it is ambiguous.
Regards,
Manfred.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the second example fails because String and StringBuffer are not related classes. That is, they are on the same level in the hierarchy. So, as Manfred said the call is ambiguous and consequently the compiler complains.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I agreed that JVM is treating 'null' as string, but when I ran the program by commenting the method which takes String as parameter, the out put is 'Object Verion', what it mean? does JVM treating 'null' as object reference in this case?( in the absence of method(String s) ).
that mean JVM is giving priority to method(String s) rather than method(Object o).
I wish some one can explain this in more detail.
Rammohan
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rammohan Meda,
Whats happening here is that the JVM looks for the most specific match and in the first case it wouls be String,but since in the second case,bot String and StringBuffer are in the same level of heirarchy,the JVM complains
Rashmi
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Rashmi said...
by most specific the compiler is looking for the clas that is the most derived (lowest in the hierarchy) argument in the method. And, like Rashmi said, both String and StringBuffer are at the same level so it doesn't know which one to pick.

Dave
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This same discussion is over on erratta:
http://www.javaranch.com/ubb/Forum35/HTML/000344.html
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic