• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between calling toString() and appending an empty string

 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Fredrik Larsback
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're absolutely right.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks people, It was a good learning time for me, from all of you..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic