String concatenation operator

The arithmetic + operator that perform addition on numbers is also used as a concatenation operator in Java.

The operator concatenates Strings like this:

However, the operator doesn’t just perform concatenations of strings, it can also concatenate numbers and objects, for example:

This translates the integer i to a string and it is concatenated with the s1 and s2 strings, forming a new string looking like:

If you need to make calculations during concatenation of strings, be sure to put them within parentheses:

If concatenating objects the method toString() of the base class java.lang.Object is called and the value returned is concatenated to the string.

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?