0

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"};
7
  • 2
    What has the last line, the string array, got to do with the rest of your question? Commented May 13, 2021 at 20:11
  • java9 is already outdated and out of support, why not go for at least some supported version like java 11. and val keyword isn't even java? Commented May 13, 2021 at 20:14
  • 4
    You could also treat that whole thing as a string literal with var x = "A\u00ea\u00f1\u00fcC" Commented May 13, 2021 at 20:14
  • I'd like to talk to the management about wasting development time on trivial updates that have no effect on the end product. Commented May 13, 2021 at 20:15
  • indeed, if you've done changes like these for several years, your situation seems really bad. Commented May 13, 2021 at 20:16

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.