• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between calling toString() and appending an empty string

 
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're absolutely right.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks people, It was a good learning time for me, from all of you..
 
reply
    Bookmark Topic Watch Topic
  • New Topic