01 / 21
"Abc"
\(+\)

String Creation
& Concatenation

Learn how to create, store, and join strings in Java: from literal declarations to the \(+\) operator and beyond.

Java

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.

String greeting = "Hello, World!"; System.out.println(greeting); // Hello, World!

Two Ways to Create a String

String Literal
Assign a value directly with double quotes. Java checks the String Pool first and reuses existing objects.
new Keyword
Use new String(...) to explicitly create a new object on the heap, bypassing the pool.
// Literal: uses the String Pool String a = "Java"; // new keyword: always creates a new object String b = new String("Java");

The String Pool in Memory

HEAP MEMORY STRING POOL "Java" "Hello" new String("Java") Variable a Variable c Variable b a and c point to the same object

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.

"Hello"
\(+\)
" World"
\(=\)
"Hello World" operand 1 operand 2 new String

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.

String first = "AP"; String second = "CS"; String result = first + " " + second; // result is "AP CS"

The .concat() Method

Besides \(+\), the String class provides a .concat() method that appends one string to another. The result is always a new String.

String greeting = "Hello"; String full = greeting.concat(" World"); System.out.println(greeting); // "Hello" (unchanged) System.out.println(full); // "Hello World"

\(+\) 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.

System.out.println("Score: " + 90 + 5); // "Score: 905" (not 95!) System.out.println(90 + 5 + " points"); // "95 points" (addition happens first)

Step-by-Step: Building Strings

1
Declare the variable with the keyword String (capital S).
2
Assign a value using a literal ("...") or the new keyword.
3
Concatenate with \(+\) to join strings and/or auto-convert primitives.
4
Parenthesise any arithmetic that must evaluate before concatenation.
5
Store or print the result: remember, the original Strings are unchanged.

Example 1: Simple Greeting

String name = "Alice"; int age = 17; String message = "Hi, " + name + "! You are " + age + " years old."; System.out.println(message);

Example 2: Order of Operations

int x = 3; int y = 4; System.out.println("Sum: " + x + y); // "Sum: 34" System.out.println("Sum: " + (x + y)); // "Sum: 7"
Without parentheses "Sum: "
\(+\)
3
\(=\)
"Sum: 3"
\(+\)
4
\(=\)
"Sum: 34" With parentheses "Sum: "
\(+\)
(3 + 4)
\(=\)
"Sum: "
\(+\)
7
\(=\)
"Sum: 7"

Example 3: Multi-Line Build

String item = "Widget"; int qty = 5; double price = 9.99; String invoice = "Item: " + item + " | Qty: " + qty + " | Total: $" + (qty * price); System.out.println(invoice);

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
String quote = "She said \"hello\" to me."; System.out.println(quote); // She said "hello" to me.

Common Mistakes to Avoid

✕ Forgetting Spaces
Writing "Hello" + "World" yields "HelloWorld" with no space. Always include a space in one of the operands.
✕ Missing Parentheses
Writing "Total: " + 3 + 4 gives "Total: 34" instead of "Total: 7". Wrap arithmetic in parentheses.
✕ Using \(==\) for Content
\(==\) compares object references, not content. Use .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?

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");

Question 5 of 5

Which of the following correctly produces the output "Total: 15"?