Java 9 integer for String cast

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

So my question is, are there any other drawbacks in doing int for String as follows?

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 (needed for this), I changed it. I am interested in the positive or negative sides of various castings. And everyone must decide for himself which one to use.

+7
java java-9
source share
1 answer

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

In particular, if i is a compile-time constant, ""+i will also be, contrary to String.valueOf(i) . Of course, if the definition is similar to final int i=16; readers will object to using ""+i , as "16" will be much clearer. But if it was about final int DEFAULT_USER = DEFAULT_GROUP << GROUP_BIT; , ""+DEFAULT_USER much clearer than a literal string containing the actual number. And being a compile-time constant is not only a performance issue, but it also allows you to use a string in the annotation or case shortcuts of the switch .

If i not a compile-time constant, there is no compiled form required, therefore, in principle, compilers are allowed to compile ""+i in Integer.toString(i) in any case. If we compare the usual naive (or call it "straightforward") implementation of new StringBuilder().append("").append(i).toString() with Integer.toString(i) or a hypothetical optimized implementation of Java 9, then only final copy action from StringBuilder s buffer to result An array of String s values ​​may have a performance impact, but it can be optimized by the JVM HotSpot. Another problem Java 9 is StringBuilder is the initial capacity of StringBuilder , which is not appropriate here, since the string representation of int easily fits into the default capacity of 16 char s.

For most non-trivial int values, decimal conversion costs outweigh the other costs, so if you prefer ""+i over Integer.toString(i) , you shouldn't have performance issues to avoid using This. It also means that you should not expect sharp acceleration with the implementation of Java 9. The main operation remains the same.

I think the biggest improvement in Java 9 solution is to reduce the size of the code, since all these similar call sequences generated for each string concatenation expression will be reset to one instruction (this is especially true for multiple expression concatenations). The ability to improve performance is just a nice addition, but I would not expect the improvements to be dramatic, especially in the first versions of JRE 9.

So the solution between ""+i and Integer.toString(i) or String.valueOf(i) is just a stylistic issue (which we are not discussing here), and not a performance issue.

+5
source share

All Articles