Blessed Geek
Blessed Geek

Reputation: 21654

Which toString technique is more efficient?

I have a class called Zebra (not her actual name). Zebra overrides the toString method to provide her own convoluted obfuscated stringification.

Which is more efficient to stringify an instance of Zebra? Presuming that I have to do this stringification millions of times per session.

  1. zebra.toString()
  2. ""+zebra
  3. static String BLANK (singleton)
    BLANK+zebra (multiple executions).

Where the value of zebra is not assured to be the same.

I am conjecturing that the answer could be - no concern: the compiler makes them all equivalent. If that is not the answer, please describe the instantiation process that makes them different. (2) and (3) could be the same, since the compiler would group all similar strings and assign them to a single reference.

Normally, I do ""+zebra because I am too lazy to type zebra.toString().

ATTN: To clarify.

I have seen questions having been criticised like "why do you want to do this, it's impractical" If every programmer refrains from asking questions because it has no practical value, or every mathematician does the same - that would be the end of the human race.

If I wrote an iteration routine, the differences might be too small. I am less interested in an experimental result than I am interested in the difference in processes:

For example, zebra.toString() would invoke only one toString while, "+zebra would invoke an extra string instantiation and and extra string concat. Which would make it less efficient. Or is it. Or does the compiler nullify that.

Please do not answer if your answer is focused on writing an iterative routine, whose results will not explain the compiler or machine process.

Virtue of a good programmer = lazy to write code but not lazy to think.

Upvotes: 0

Views: 949

Answers (7)

Jay
Jay

Reputation: 27474

Number 1 is more efficient.

The other options create an instance of StringBuilder, append an empty string to it, call zebra.toString, append the result of this to the StringBuilder, and then convert the StringBuilder to a String. This is a lot of unnecessary overhead. Just call toString yourself.

This is also true, by the way, if you want to convert a standard type, like Integer, to a String. DON'T write

String s=""+n; // where "n" is an Integer

DO write

String s=n.toString();

or

String s=String.valueOf(n);

Upvotes: 2

Steve Kuo
Steve Kuo

Reputation: 63084

zebra.toString() is the best option. Keep in mind zebra might be null, in which case you'll get a NullPointerException. So you might have to do something like

String s = zebra==null ? null : zebra.toString()

""+zebra results in a StringBuilder being created, then "" and zebra.String() are appended separately, so this is less efficient. Another big difference is that if zebra is null, the resulting string will be "null".

Upvotes: 1

gnomed
gnomed

Reputation: 5565

As a general rule, I would never use the + operator unless it is on very small final/hard-coded strings. Using this operator usually results in several extra objects in memory being created before your resulting string is returned (this is bad, especially if it happens "millions of times per session").

If you ever do need to concatenate strings, such as when building a unique statement dynamically (for SQL or an output message for example). Use a StringBuilder!!! It is significantly more efficient for concatenating strings.

In the case of your specific question, just use the toString() method. If you dont like typing, use an IDE (like eclipse or netbeans) and then use code completion to save you the keystrokes. just type the first letter or 2 of the method and then hit "CTRL+SPACE"

Upvotes: 1

A.H.
A.H.

Reputation: 66263

If you want to know things like this you can code small example routines and look at the generated bytecode using the javap utility.

I am conjecturing that the answer could be - no concern: the compiler makes them all equivalent. [...] Normally, I do ""+zebra because I am too lazy to type zebra.toString().

Two things:

First: The two options are different. Think about zebra being null.

Second: I'm to lazy to do this javap stuff for you.

Upvotes: -1

mre
mre

Reputation: 44240

Since the object's toString method will be invoked implicitly in cases where it is not invoked explicitly, a more "efficient" way doesn't exist unless the "stringification" is happening to the same object. In that case, it's best to cache and reuse instead of creating millions of String instances.

Anyway, this question seems more focused on aesthetics/verbosity than efficiency/performance.

Upvotes: 0

DwB
DwB

Reputation: 38300

Option 1 is the best option since every option calls the toString() method of zebra, but options 2 and 3 also do other (value free) work.

  1. zebra.toString() - Note that this calls the toString() method of zebra.
  2. ""+zebra - This also calls the toString() method of zebra.
  3. static String BLANK; BLANK+zebra; - This also calls the toString() method of zebra.

You admit "I'm lazy so I do stupid stuff". If you are unwilling to stop being lazy, than I suggest you not concern yourself with "which is better", since lazy is likely to trump knowledge.

Upvotes: 0

Saurabh
Saurabh

Reputation: 7964

If the Zebra is Singleton class or the same instance of zebra is being used then you can store the result of toString in Zebra and reuse it for all future calls to toString.

If its not the case then in implementation of toString cache the part which is unchanges everytime in constructing String at one place, this was you can save creating some string instances every time.

Otherwise I do not see any escape from the problem you have :(

Upvotes: 0

Related Questions