• Post Reply Bookmark Topic Watch Topic
  • New Topic

About String & StringBuffer  RSS feed

 
pias Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this will not compile?
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); }}
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pias Khan:
question.method(null);


Why are you doing this?! I mean, why is it that you are passing null?!

Also, when you post your code, please use the tags.
 
Mani Venkatesan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pias Khan:
Why this will not compile?
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); }}


If you specify "null", how will the compiler know which method to pick? There is an ambiguous reference and hence this code does not compile.
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy! Yeah, it looks like you found yourself a one-way ticket to compiler hell! I'd say tryin' this instead:



Good luck!

[ August 03, 2004: Message edited by: Jack Kay ]
[ August 03, 2004: Message edited by: Jack Kay ]
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Kay:
Howdy! Yeah, it looks like you found yourself a one-way ticket to compiler hell!


Good one
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Kay:
Howdy! Yeah, it looks like you found yourself a one-way ticket to compiler hell! I'd say tryin' this instead: [...]
I would really, really object against treating null and the empty string as interchangeable. It completely flies in the face of best practice (see Joshua Bloch's Effective Java, item 27, and the null object pattern).

It's easy enough to disambiguate the method call: - Peter
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,

What you say is perfectly right. But I don't understand under what circumstances would such a syntax be required?:

Originally posted by Peter den Haan:
- Peter


I mean, when would you chose to type cast a null into some kind of an object. I am asking you this because this is the first time I am seeing this syntax.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vijayendra V Rao:
[...] But I don't understand under what circumstances would such a syntax be required? [...]
Not sure I understand your question. Under circumstances like the one above: where you want to call an overloaded method using the "null" literal as a parameter, and the compiler needs you to specify the type so it knows which method you mean.Is more or less the same asThis no doubt looks more familiar...

The underlying problem is that the "null" literal is untyped. Most of the time, that's no problem, but when calling overloaded methods it can lead to ambiguities that you have to resolve by casting it to a specific type. It's the same cast operator you've used dozens of times before; you've just never seen it used on "null"

- Peter
[ August 03, 2004: Message edited by: Peter den Haan ]
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter den Haan:
Not sure I understand your question.


I'm sorry Peter! Maybe I did not put forth my question clearly

But your explanation dis clear the doubt I had in mind. Thanks anyway
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clear things up, the question is sometimes we NEED to pass a null value, either we don't want the option for some reason, or some other reason that doesn't pop up everyday.

But if we have two methods that take null values, then this will not work:



So like Peter said, do this if you want to pass a specified null value:


And I hope everyone understands now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!