The “+” and “+ =” operators that are mentioned here have some special aspects when used with String object in Java. In this tutorial, I would like to present you those aspects!
First of all, I need to say that the “+” and “+ =” operators can be used to concatenate two String strings.
For example:
The implementation of the concatenation will be done from left to right, so in the below case you have to pay attention if not, there will be a mistake.
For example:
1 2 3 |
int a = 2; int b = 3; String c = a + b + "Khanh"; |
Because a and b are primitive variables, they can be manipulated with the “+” operator, so in this case the result would not be 23Khanh, it would be 5Khanh.
But if you now change the operation a bit, the result will be different.
For example:
When concatenating two strings, one of which is null, null is the value of the null string.
For example: