Joining text
Description
Concatenation combines values into a message.
Explanation
When one operand is a String, + converts the other value to text.
Example
String message = "Hello, " + "Aditi";← Chapter 3 — Strings, Escapes & Math
mixed
Strings let a program store labels, names, messages, and other text. Concatenation joins pieces of text with the + operator.
String methods return information or a new String. They do not change the original String because String objects are immutable.
Concatenation combines values into a message.
When one operand is a String, + converts the other value to text.
String message = "Hello, " + "Aditi";length, toUpperCase, and contains inspect text.
Store a returned value if you need the changed version.
String shout = name.toUpperCase();For a few pieces of text, + is clear. For many changes in a loop, StringBuilder is usually a more efficient choice.
public class StringMethodsDemo {
public static void main(String[] args) {
String name = "Rohan";
String greeting = "Hello, " + name.toUpperCase();
System.out.println(greeting);
}
}