Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 9 integer to String cast

Tags:

java

java-9

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.

like image 926
Mein Name Avatar asked Oct 31 '16 16:10

Mein Name


People also ask

Can I cast an int to a string Java?

We can convert int to String in java using String.valueOf() and Integer.toString() methods. Alternatively, we can use String.format() method, string concatenation operator etc.

Can integer be cast to string?

Method 1: Using toString Method of Integer Class The Integer class has a static method that returns a String object representing the specified int parameter. The argument is converted and returned as a string instance. If the number is negative, the sign will be preserved.

Can you cast to a string in Java?

We can convert a char to a string object in java by using the Character. toString() method.


1 Answers

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.

like image 110
Holger Avatar answered Oct 10 '22 13:10

Holger