*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding String 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 "Regarding String" Watch "Regarding String" New topic
Author

Regarding String

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Jul 23, 2001
Posts: 49
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

Joined: Jul 18, 2001
Posts: 4
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

Joined: Jun 25, 2001
Posts: 50
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

Joined: May 10, 2001
Posts: 3244
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


Dave
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding String