Skip Top Navigation Bar
Current Tutorial
Create and assign simple strings of characters

Create and assign simple strings of characters

Snippet: Concatenating a String with another String, and reassigning the result

Explanation

This snippet shows how to concatenate a string and reassign the result to the same variable. The variable message starts with the value "Hello". Then, message + " World" creates a new string "Hello World", and the result is assigned back to message.

After the reassignment, message holds "Hello World". The original "Hello" string is not modified — strings in Java are immutable. The variable simply now points to a new string value.

What You Can Do

Try concatenating more words — can you build a full sentence step by step, reassigning message each time?

You can also try using the += shorthand: message += " World" does exactly the same thing as message = message + " World". Does the output change?

Run it and observe the output!


Last update: May 1, 2026


Current Tutorial
Create and assign simple strings of characters