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]
# 1

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

}

}

georgemca at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 2

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

Jitu-javaa at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 3

what's the motive behind this question, and why would anyone want to call a method in such an ambiguous fashion?

petes1234a at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 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. But I'm with Pete: what the h3ll are you trying to do?

georgemca at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 5

I think it's just an academic question.

java_knighta at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 6

> 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!

georgemca at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 7

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

Jitu-javaa at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 8

> sorry anyways

You don't have to :-)

java_knighta at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 9

> 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 > top of Java-index,Java Essentials,New To Java...
# 10

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());

}

}

Prem_Sa at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 11

> public class AQuestion

> {

Is there some point?

jverda at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 12

good explanation. Btw why it printed "String version" when called with null?

prassoona at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 13

> good explanation. Btw why it printed "String version"

> when called with null?

Reply #4.

~

yawmarka at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 14

> > 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

prassoona at 2007-7-29 11:58:14 > top of Java-index,Java Essentials,New To Java...
# 15

> 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!

Hippolytea at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 16

> 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 > top of Java-index,Java Essentials,New To Java...
# 17

> > > 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.

~

yawmarka at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 18

thanks folks. Java choose String version just because alphabetically String comes before StringBuffer?

prassoona at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 19

> 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);

}

}

~

yawmarka at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 20

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..

prassoona at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 21

> 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!

~

yawmarka at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 22

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");

}

kapoorvinnya at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 23

Same question. Same answer.

ejpa at 2007-7-29 11:58:22 > top of Java-index,Java Essentials,New To Java...
# 24

> 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 > top of Java-index,Java Essentials,New To Java...