Concatenation operator and String object in Java

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:

Concatenation operator and String object in Java

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:

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.

Concatenation operator and String object in Java

But if you now change the operation a bit, the result will be different.

For example:

Concatenation operator and String object in Java

When concatenating two strings, one of which is null, null is the value of the null string.

For example:

Concatenation operator and String object in Java

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment