Confusion Between String and StringBuffer
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);
}
}
Why does the output shows the Compile Time Error
[456 byte] By [
Jitu-javaa] at [2007-11-27 10:55:43]

Nothing to do with String and StringBuffer. The compiler can't work out which method you're trying to invoke, since 'null' doesn't have a type. You could cast it to be more specific if you really wanted
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((String)null); // note the cast
}
}
Thx for Reply Buddy But I 've some more confusion
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);
}
}
The Output of the following is "String Version". Although we're calling a method with a null parameter without any Type Casting
what's the motive behind this question, and why would anyone want to call a method in such an ambiguous fashion?
Well, now that one of them takes an object, the compiler is able to make a guess as to what method you want, by taking the most specific method possible. But I'm with Pete: what the h3ll are you trying to do?
I think it's just an academic question.
> I think it's just an academic question.
Well, obviously. If it were a legitimate problem, he wouldn't have the option of just randomly changing the method signature to take whatever type he pleases!
I know man. it is like a Academic Question. The Idea I want to convey is that how a Compiler will behave in various Situations. I hope all of u dont mind if I Put some questions which might not be great for the practical Purposes but are intereseting academic Questions..
sorry anyways
> sorry anyways
You don't have to :-)
> Nothing to do with String and StringBuffer. The
> compiler can't work out which method you're trying to
> invoke, since 'null' doesn't have a type.
<pb>
Actually, null's type is the null type.
The reason it complains here is because none of the signatures that match is more specific than any other. If their respective args, were, say, Parent, Child, and Grandchild, then the one that takes Grandchild would be invoked.
</pb>
jverda at 2007-7-29 11:58:14 >

public class AQuestion
{
public void method(String s)
{
System.out.println("String Version");
}
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(new String());
question.method(new StringBuffer());
}
}
> public class AQuestion
> {
Is there some point?
jverda at 2007-7-29 11:58:14 >

good explanation. Btw why it printed "String version" when called with null?
> good explanation. Btw why it printed "String version"
> when called with null?
Reply #4.
~
> > good explanation. Btw why it printed "String
> version"
> > when called with null?
>
> Reply #4.
>
> ~
yawmark- did you mean "what's the motive behind this question, and why would anyone want to call a method in such an ambiguous fashion?"
The question is not about the need of such a call but just curious to know why java allowed such a call? Why didnt it throw error? That method should accept only arguments of String type, but here it accepts a null type!!!
Message was edited by:
prassoon
> That method should accept only arguments of String type, but here it accepts a null type!!!
Are you saying this *should* be a compile-time eror?!
class Test {
public static void main(String[] args) {
test(null);
}
static void test(String s) {}
}
String is a class, so variables of type String can have the null value!
> That method should accept
> only arguments of String type,
Why? Because that's how you want it to work, or how you intuitively think it should work?
It "should" do whatever the JLS says it should do, and that's exactly what it's doing. Null is type-compatible with every reference type.
If you follow your reasoning to a logical conclusion, the only methods that could ever take null as an argument would be those declared to take exactly null. What would be the point in having a method parameter that always has to be null?
jverda at 2007-7-29 11:58:22 >

> > > good explanation. Btw why it printed "String
> > version"
> > > when called with null?
> >
> > Reply #4.
> >
> > ~
>
> yawmark- did you mean "what's the motive behind this
> question, and why would anyone want to call a method
> in such an ambiguous fashion?"
Did you actually read reply #4?
"Well, now that one of them takes an object, the compiler is able to make a guess as to what method you want, by taking the most specific method possible."
The JLS defines this behavior.
~
thanks folks. Java choose String version just because alphabetically String comes before StringBuffer?
> thanks folks. Java choose String version just because
> alphabetically String comes before StringBuffer?
No, Java chose the String method because String is more specific than Object. If the only overloaded methods take String and StringBuffer, the code won't compile due to the ambiguity of the arguments.
Example (won't compile):class Foo {
public static void main(String[] args) throws Exception {
m(null); // < ambiguous; both arguments are equally specific
}
static void m(String a) {
System.out.println(a);
}
static void m(StringBuffer a) {
System.out.println(a);
}
}
Example (will compile and run):class Foo {
public static void main(String[] args) throws Exception {
m(null); // ]]> null
}
static void m(String a) { // most specific choice
System.out.println("]]> " + a);
}
static void m(Object a) {
System.out.println("--> " + a);
}
}
~
Great you replied so fast.. i was supposed to edit my question...now its clear.. since String is the subclass of Object, the one with String argument will be called..
> Great you replied so fast.. i was supposed to edit my
> question...now its clear.. since String is the
> subclass of Object, the one with String argument will
> be called..
You got it!
~
Thx for Reply Buddy But I 've some more confusion
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);
}
}
The Output of the following is "String Version". Although we're calling a method with a null parameter without any Type Casting
yes the output is "String Version". bcoz String class inherits the Object class
thus calling question.method(null); would result to the more specific overloaded method which is
public void method(String s)
{
System.out.println("String Version");
}
Same question. Same answer.
ejpa at 2007-7-29 11:58:22 >

> The Output of the following is "String Version".
> Although we're calling a method with a null parameter
> without any Type Casting
Did you even bother to read what anybody else wrote?
jverda at 2007-7-29 11:58:22 >
