String Creation
& Concatenation
Learn how to create, store, and join strings in Java: from literal declarations to the \(+\) operator and beyond.
What Is a String?
A String in Java is a sequence of characters enclosed in double quotes. Unlike primitive types such as int or boolean, a String is an object: an instance of the java.lang.String class.
Two Ways to Create a String
new String(...) to explicitly create a new object on the heap, bypassing the pool.The String Pool in Memory
What Is Concatenation?
Concatenation means joining two or more strings end-to-end to produce a new string. In Java, the primary way to concatenate is with the \(+\) operator.
The \(+\) Operator for Strings
Java overloads the \(+\) operator so it works on Strings as well as numbers. When at least one operand is a String, \(+\) performs concatenation rather than arithmetic.
The .concat() Method
Besides \(+\), the String class provides a .concat() method that appends one string to another. The result is always a new String.
\(+\) Operator vs .concat()
| \(+\) Operator | .concat() Method | |
|---|---|---|
| Syntax | \(a + b\) | a.concat(b) |
| Handles null? | Converts null to "null" |
Throws NullPointerException |
| Non-String operands | Auto-converts to String | Only accepts String |
| Chaining | \(a + b + c\) | a.concat(b).concat(c) |
| Common usage | Most common | Rare in practice |
Mixing Numbers and Strings
When \(+\) encounters a String and a number, it converts the number to its String representation and concatenates. Java evaluates left to right, which can produce surprising results.
Step-by-Step: Building Strings
String (capital S)."...") or the new keyword.Example 1: Simple Greeting
Example 2: Order of Operations
Example 3: Multi-Line Build
Escape Characters in Strings
Some characters cannot appear directly inside double quotes. Java uses a backslash (\) to introduce escape sequences.
| Sequence | Meaning | Example Output |
|---|---|---|
\n |
New line | Line 1 Line 2 |
\t |
Tab | Col1 Col2 |
\" |
Double quote | She said "hi" |
\\ |
Backslash | C:\Users |
Common Mistakes to Avoid
"Hello" + "World" yields "HelloWorld" with no space. Always include a space in one of the operands."Total: " + 3 + 4 gives "Total: 34" instead of "Total: 7". Wrap arithmetic in parentheses..equals() to compare String values.Summary of Key Concepts
| Concept | Syntax / Detail | Key Point |
|---|---|---|
| Literal creation | String s = "hi"; | Uses String Pool |
| new keyword | String s = new String("hi"); | Always new object |
| \(+\) operator | \("a" + "b"\) | Most common concatenation |
| .concat() | "a".concat("b") | Method-based, no null safety |
| Immutability | Strings never change | Operations return new Strings |
| Auto-conversion | "val: " + 42 | Primitives become Strings |
Question 1 of 5
What is the output of the following code?
System.out.println("Java" + 1 + 2);
Question 2 of 5
Which statement correctly creates a String using a literal?
String with a capital S. Option A has a lowercase s, B uses the new keyword, and D uses single quotes (which are for char in Java).Question 3 of 5
What is the output of:
System.out.println(4 + 3 + "hello");
Question 4 of 5
After the following code runs, what does word contain?
String word = "Hi"; word.concat(" there");
.concat() method returns a new String, but the result is not assigned back to word. So word still holds "Hi".Question 5 of 5
Which of the following correctly produces the output "Total: 15"?
* has higher precedence). Option C does not compile because .concat() only accepts a String, not an int. Option D uses parentheses to force addition first: (10 + 5) = 15, then concatenation produces "Total: 15".