8

I just read about this Java 9 feature https://bugs.openjdk.java.net/browse/JDK-8085796 saying that the 'slow' String concatenation using StringBuilder will be improved.

So my question is, whether there are still any downsides of doing int to String casts the following way?

int i = 16;
String s = ""+i;

Or are there any pros/cons of casting int to String using Integer.toString(i) or String.valueOf(i)?

Edit: Since my question was too opinion based (sry for that) I changed it. I am interested in positive or negative sides of the different castings. And everyone should decide for themself which one to use.

8
  • 15
    Shorter, yes. Easier to understand? I don't think so. Integer.parseInt is a red herring as it goes in the opposite direction - String.valueOf(i) says exactly what it's doing. Your aim is not to perform string concatenation, so why would your code include string concatenation? Commented Oct 31, 2016 at 16:44
  • 1
    Improved doesn't mean it's as fast. Personally I prefer using ""+i (Unless you know there is a performance issue) but it's a matter of taste Commented Oct 31, 2016 at 16:44
  • 2
    @PeterLawrey: I suspect we'll have to agree to disagree. I'm fine with Integer.toString - both that and String.valueOf say what they're trying to do, with the advantage of String.valueOf being that you can use it for all types. Commented Oct 31, 2016 at 16:47
  • 2
    And I'd say that yes, there's still a downside: your code looks like you're interested in string concatenation, but you're actually wanting to get the string representation of a value. Commented Oct 31, 2016 at 16:48
  • 5
    I don't see how string concatenation, even with a better strategy allowed in the future by the indyfication of the code, could ever be faster than converting an integer to a string (with Integer.toString(), for example), since it will have to convert anyway, and then concatenating with the empty string. At best, it could detect this ugly (IMHO) idiom and translate it to Integer.toString(). But I'm with Jon Skeet here: Integer.toString expresses the intent clearly. Commented Oct 31, 2016 at 16:56

1 Answer 1

6

First of all, the assumption that Integer.toString(i) is always faster than ""+i does not hold.

Most notably, if i is a compile time constant, ""+i will be as well, contrary to String.valueOf(i). Of course, if the definition is like final int i=16;, readers will object the use of ""+i, as "16" would be much clearer. But if we’re talking about final int DEFAULT_USER = DEFAULT_GROUP << GROUP_BIT;, ""+DEFAULT_USER is much clearer than a literal string containing the actual number. And being a compile-time constant is more than just a performance issue, it allows using the string in annotations or case labels of a switch statement.

If i is not a compile-time constant, there is no mandatory compiled form, so in principle, compilers are allowed to compile ""+i to Integer.toString(i) anyway. If we compare the usual naive (or let’s call it “straight-forward”) implementation new StringBuilder().append("").append(i).toString() with the Integer.toString(i) variant or the hypothetical optimized Java 9 implementation, only the final copying action from the StringBuilder’s buffer to the result String’s value array may have a performance impact, but this can get optimized away by the HotSpot JVM. The other issue, that the Java 9 solution aims at, the StringBuilder’s initial capacity, is not relevant here, as an int’s string representation easily fits into the default capacity of 16 chars.

For most nontrivial int values, the costs of the conversion to the decimal form will outweigh the other costs significantly, so if you prefer ""+i over Integer.toString(i) you should not let performance concerns be the reason not to use it. This also implies that you should not expect a dramatical speedup with the Java 9 implementation. The main operation is still the same.

I think, the biggest improvement of the Java 9 solution is the reduction of the code size, as all these similar invocation sequences, generated for every string concatenation expression, will be fold to one instruction (that’s especially relevant to concatenations of multiple expressions). The opportunity to improve the performance, is only a nice add-on, but I wouldn’t expect the improvements to be dramatic, especially not in the first versions of JRE 9.

So the decision between ""+i and Integer.toString(i) or String.valueOf(i) is merely a stylistic question (which we don’t discuss here), not a performance issue.

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

1 Comment

The compile time constant fact is verry interesting. Thanks a lot for this detailed answer.

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.