aspose file tools*
The moose likes Beginning Java and the fly likes Difference between calling toString() and appending an empty string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between calling toString() and appending an empty string" Watch "Difference between calling toString() and appending an empty string" New topic
Author

Difference between calling toString() and appending an empty string

Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

I was just curious to know the difference between the following usages


Though the result of last two lines are same, I would like to know which one is better in terms of performance etc.

Similarly

What are the other ways I can use to convert this integer to String effectively

Thanks in advance
Fredrik Larsback
Greenhorn

Joined: Dec 21, 2009
Posts: 9
Though the result of last two lines are same, I would like to know which one is better in terms of performance etc.


Correct me if I'm wrong but I think that creates a StringBuilder behind the scene and appends the empty String and a.toString together.
Performance wise there is no difference between the two, what's important for the performance point of view is the implementation of the toString() method.
The cleanest way to implement is just to do .

What are the other ways I can use to convert this integer to String effectively



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

For any type of a (int, float, Object, Integer, etc), ""+a is equal to this in Java 6:
For all object types "append(a)" is equal to "append(String.valueOf(a))" with String.valueOf being somewhat like this: So if you do ""+a, you also call a.toString(), but with another String (""), a StringBuilder, and the final String. So yes, a.toString() is more efficient.

If you want to be safe, you can always call String.valueOf(a). This will also handle the cases where a is null, or where a is a primitive type (String.valueOf has been overloaded for these).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fredrik Larsback
Greenhorn

Joined: Dec 21, 2009
Posts: 9
You're correct Rob.
But I'd say it's micro optimization. If you've got performance problems in your code it isn't because of how you implement your System.out.println.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

I agree but using String.valueOf instead of concatenate using an empty String is always a better solution; to me it makes it clearer what you want to do - get a String value of something.
Fredrik Larsback
Greenhorn

Joined: Dec 21, 2009
Posts: 9
You're absolutely right.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Prime wrote:If you want to be safe, you can always call String.valueOf(a). This will also handle the cases where a is null


Well Rob,
this one is Handling the null



but here fails


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

That's because there are two matches for null: String.valueOf(Object) and String.valueOf(char[]). If you pass null, the compiler finds the most specific, and that is valueOf(char[]). Unlike String.valueOf(Object), that one does throw an exception. The following will work fine:
Almost the same, but this time the reference type (Object) makes sure that String.valueOf(Object) is called instead of String.valueOf(char[]).
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks a lot Rob
Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

Thanks people, It was a good learning time for me, from all of you..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between calling toString() and appending an empty string