programming

Java Compiler String Addition

Lately I have seen quite a few misconceptions regarding how string concatenation is handled in the Java world so I would like to write this short blog entry with a couple of stupid examples in order to show the basics of how it is done. Note: I am focusing here in the bytecode generated by the Java compiler, regardless any optimization the runtime could apply. Concatenating constant strings Imagine we are writing a new class where we have defined three final string fields and we want to add a new method which just returns the sum of the three: