Tn general, Java does not allow operators to be applied to St,ring objects. The one , exception to this rule is the + operator, which concatenates two strings, producing ‘a String object as the result. This allows you to chain together n series of + operations . For example, the following fragment concatenates three strings:
This displays the string “He is Connors old. One of string concatenation is found when you are creating very long strings, Instead of letting long strings wrap around within your source code.you can break them into smaller unsuspecting the + to concatenate them. Here is an example:
String Concatenation with Other Data Types
You can concatenate strings with other types of data. For example, consider this .slightly different version of the counterexample: In this case, nee is an int rather them another String, but the output produced IS the same as before. This is because the int value in age is automatically converted into its string representation within a String object. This string is then concatenated as before, The compiler will convert an operand to its string equivalent whenever the other operand of the + is m instance of String. Be careful when you mix other types of operations with string concatenation expressions, however. You might get surprising results. Consider tile following that you probably expected. Here’s why. Operator precedence causes the concatenation of “four” with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 2 a second time. To complete the integer addition first, you must use parentheses, like this.