Welcome to the second episode of this little Java-unconfusing series.
Today I would like to talk briefly about String concatenation operator +
.
First, let me share some interesting use-case, then we will take a look under the hood to understand what happened.
Example
Let’s take a look at the following piece of code:
What do you think this code will output?
What happened
If we look at the bytecode, we will see a very boring piece:
We see that the String evaluation doesn’t happen at runtime,
it has happened at compile time. The whole expression got optimized away,
so we’re dealing with a constant value: 6text51
.
But how did this value come to be?
Going from left to right, only two first operands are taken into account at a time.
If none of them is String
, and the numeric operation can be performed, it is performed.
If any of the two is String
, the other operand will also be converted to a String
, and they will be concatenated.
So, in our example, first operation is 1 + 5
, which can be evaluated as 6
.
Next, we will have 6 + "text"
, which will result in concatenated String "6text"
.
Afterward, all the operations will be resulting in String concatenation,
bringing us to the final result: "6text51"
.
Let’s try something out
Let’s try out something strange. As mentioned, the evaluation happens at compile time. What if we force it to happen at the runtime?
For example, we can create a situation where the exact type of some value is not known at compile time.
It looks ugly, but bear with me:
Based on what we discussed before, we know that here it doesn’t matter
what is the type of the unknown
variable, because the second operand is String
,
so the first two operands will be concatenated.
This is the relevant piece of bytecode:
To summarize the above bytecode: we don’t know at compile time,
what’s the type of unknown
variable. However, no matter what it is,
it’s going to be ultimately converted to a String
, because the next operand in our expression
is String
, so they will be concatenated in any case.
Now the interesting part. What if we make the second operand non-String?
Now, the compiler knows that it’s not necessarily a String concatenation. It can be something else depending on the type, but the type of the first operand is unknown… What would you do if you were a compiler?
The correct answer is: complain :) This simply won’t compile.
Lessons learned
String concatenation is a very interesting operation, because it leverages a lot of optimizations both at compile time and at runtime.
Today we saw how the compile time evaluation happens: two operands at a time, from left to right. And if you’re giving your compiler hard time deciding what operation it should perform, it will shout at you.
Hope this was fun and educative! See you in the next one!
P.S.: If you liked this one, chances are you’d also like the one about increments.