• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding String

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • 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 ?
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • 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.
 
Alex Sbityakov
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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.
 
Rammohan Meda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • 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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic