AaronB
AaronB

Reputation: 21

Java - use new String( ) or valueOf(char[])

I am upgrading an application from Java 8 to Java 9. Work was started several years ago by someone no longer with the company.

They replaced uses of new Integer(int) with Integer.valueOf() I understand this has advantages in terms of avoiding unnecessary object, and have read several posts on the subject including Joshua Bloch on Considering static factory methods instead of constructors

is there any value to replacing code like val x = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C") with

String.valueOf(new char[] { 'A' , '\u00ea' , '\u00f1' , '\u00fc' , 'C' })

similarly how, if at all would you update

new String[]{"alpha", "beta", "plabelColor"};

Upvotes: 0

Views: 124

Answers (3)

kaya3
kaya3

Reputation: 51162

There seems to be no reason to replace new String(char[]) with String.valueOf(char[]), and in fact the latter is likely implemented by just doing the former - here's the source code of the String class from JDK 9, for example (link):

    public static String valueOf(char data[]) {
        return new String(data);
    }

So the only difference would be one extra method call (which at best would be inlined by the JIT). On the other hand, if there is a difference then it would be extremely small, and not worth considering, so instead of thinking about performance you should probably choose based on which way you think is more readable.

That said, the code in your example is not using the new String(char[]) constructor, it is using the new String(String) constructor to copy a string. This is not needed, you can just write the expression which creates the string without calling the constructor, unless there is some reason your code requires the string object to not be interned (and if it does, that smells like a bad design to me.)


If you do think there might be some benefit in caching frequently-used strings, you should implement a cache yourself, design it to be optimised for the kind of strings your application is likely to be dealing with, and then benchmark it to see if it's actually faster. You'll have the cost of checking the cache every time, and this cost will likely be larger for strings than for integers, especially if it involves hashing and testing equality of strings; so if there are too many cache misses then the net effect would be negative. The cache miss rate will be highly dependent on your application, so you do want to benchmark with realistic data.

Upvotes: 0

user15793316
user15793316

Reputation:

Replacing new Integer(int) with Integer.valueOf(int) is (more than just) a good idea - new Integer(int) has been deprecated (Java 9) and marked for removal (Java 16).

val x = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C") is not really Java; if Java, I would recommend var x = "A" + "\u00ea" + "\u00f1" + "\u00fc" + "C" or even var x = "A\u00ea\u00f1\u00fcC" (unless a new, not-interned String is required (no idea why))

new String[]{"alpha", "beta", "plabelColor"} - no update needed

Upvotes: 2

Simulant
Simulant

Reputation: 20150

Don't introduce micro optimisations, if you cannot measure the impact AND require the optimisation.

We are talking about tiny micro optimisations here. If your application is not a high frequency trading application, where every nano second matters, you should favour readability of the code over micro optimisations. These optimisations might even result in the same code after compiling (or realeasing a newer compiler optimisation). These kind of optimisations hardly have any influence on your users measurable response time.

If you want to measure the impact, I suggest you use JMH for it.

Upvotes: 2

Related Questions