String Concatenation in java

Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. This allows us to chain together a series of + operations

String age = “9”;
String s = “He is ” + age + ” years old.”;
System.out.println(s);

This displays the string “He is 9 years old.”

String Concatenation with Other Data Types

String s = “four: ” + 2 + 2;
System.out.println(s);

Output

four: 22

Leave a Comment