Why string + number concatenated object, it will be a string

As one of the few number + java overloaded operators, there is provided a string concatenation function.

java -c generated jvm bytecode can be seen. The compiler is actually a manual for our new Stringbuilder to string concatenation.

So the question came. Why I was told to use the + string concatenation will produce a lot of useless objects? They are kidding me? The compiler can not optimize it for us?

In fact, not the case.

For the average string concatenation is not a problem, it will only produce a Stringbuider object. But if you splice behavior of objects in a cycle, that is to say in the form of toString is generated within a cycle,

java -c can see. + Number generated by the operation, will not produce N Stringbuider objects in the circulation. This is where we need to optimize.

That is me, when operating string concatenation occurs in the loop body, please use Stringbuider instead of the + sign.

Guess you like

Origin www.cnblogs.com/munan56/p/12111990.html