I want to perform the concatenation of two String
:
String nombre = adornoF.getNombre();
String prefijo = ga.getsid();
String all = nombre + prefijo;
As long getNombre()
as getsid()
they return a String
.
Is it ok or is there a better way?
I want to perform the concatenation of two String
:
String nombre = adornoF.getNombre();
String prefijo = ga.getsid();
String all = nombre + prefijo;
As long getNombre()
as getsid()
they return a String
.
Is it ok or is there a better way?
The ways to concatenate two
String
are:Using the operator
+
:via the concat() method :
Using the StringBuffer class and the append() method:
Using the StringBuilder class, and the append() method :
In all 3 cases you would get the same result, however as a good practice it is best to use StringBuilder to concatenate the text in the buffer and get a single object
String
usingtoString()
.Android Documentation:
That's fine, you can also spend the
concat()
java function like this:There is another way is to use
StringBuilder
In loops it is more optimal to use
StringBuilder
Extracted on OS
Here is the discussion which is better obviously depends on its use, if it is only once then better to use
+
but if it is to build a chain from a loop, then betterStringBuilder
It's very good, don't change it prematurely .
When making a program there are several things that are important, among others:
The third point is always important. In all lines of code it is important that they do what is expected of them.
The other two points are not always as important. If your concatenation of strings consumes 0.01% of the time of your program, you will gain little by optimizing it. If you consume 20% then it is worth it.
To make a program correct it is important to use code that is simple and easy to understand and maintain .
The code you have written:
meets those requirements.
Potentially more efficient code like:
It's not much harder to understand. But if you always program like this, a program with 10,000 lines will have 40,000 and there you will notice the difference.
Why hurt the readability of your program to gain an efficiency gain that might be negligible?
Conclusion: strive to keep your code simple, maintainable, and correct.
And when it works well, pass it a profiler and optimize the bottlenecks.
Is StringBuilder faster in this case?
Let's assume that, for whatever reason, we've come to the conclusion that your performance is critical. Is using StrinBuilder faster?
Let's consider the code with the + operator:
And the code with StringBuilder:
This is the code in bytecodes for the case of the + operator:
And this is the code in bytecodes for the StringBuilder case:
The numbers that come out (from 1 to 14, and those of the letters) are not real, I have modified them to make the explanation easier to understand.
The first thing that stands out is that the + operator code is compiled to a StringBuilder.
The second thing is that the StringBuilder code has the same 14 bytcode instructions as the + operator (the lines without the letter boc). Except for differences in the parameter, as in line 8 (astore_0 or astore_2), but this difference does not affect performance.
What it does affect is the 6 extra bytecode instructions (7b, 8b, 9b, 9c, 11b and 11c) that the StringBuilder code has.
It is not JVM bytecode that is delivered to the Android device. This converts to DEX. And then to native code. But with what I have seen, it is already clear. If one of these intermediate steps were to optimize the StringBuilder code, it would reach the optimized + operator code at most, but in no case would it improve it.
Conclusion: StringBuilder is slower in this particular case.
Should we disown Stringbuilder and always use the + operator?
Of course not. To concatenate two strings the + operator is better, but consider the case of an array of 1000 strings where you want to concatenate them all to make a single string.
I would dare to say that with StringBuilder it will be faster:
And not only will it be faster(1) but that code is also just as easy to understand and maintain as the alternative with the + operator:
(1) Unless the compiler is also capable of optimizing the + operator code.
One option that no one has mentioned is to use
String.format
:Result:
Well, let's do a challenge to all four. Let's say to:
Let us concatenate a string:
and let's see which one is more efficient of the 4.
Each test uses the code as it works internally.
Note :
The test has been done in Java, on a PC. It would be interesting to run it on Android (perhaps with fewer values, eg 10,000 strings)... it would be interesting to see the result.
The original code is from the 100,000 times. For other values change the variable
ITERATION
:Code:
Result (a)
Result (b)
Result (c)
Result (d)
Result (e)
What I suggest is that you use
StringBuilder
.The problem with
Strings
java is that they are immutable and every time you work with them it creates an object. With few processes, the cost is irrelevant, but with a large project,StringBuilder
it gives better performance and, in turn, less memory consumption.(although the GarbajeCollector will already take care of the latter)
Ex: