Concatenating Text and Variables

3 min read ·

Concatenation means joining text and variable values together. In Java, this is done using the + operator. It allows you to create meaningful output messages by combining strings with data.

Basic String Concatenation

You can join text and variables in a single output statement.
The text and variable value are joined and printed as one line.

Concatenating Text with Numbers

Java automatically converts numbers into text when using the + operator with strings.
The number is converted to text before printing.
Note

When a string is present, Java treats the + operator as concatenation instead of addition.


Concatenating Multiple Variables

You can join multiple variables in one statement.
Java evaluates the expression from left to right.

Order of Concatenation

The order of values matters when concatenating.
The output will be:
Because Java treats everything after a string as text.

Using Parentheses with Numbers

To perform addition before concatenation, use parentheses.
Now the output will be:

Concatenating Different Data Types

Java allows combining strings with different data types.
All values are converted to text automatically.

Common Mistakes in Concatenation

Forgetting quotes around text Expecting numbers to add without parentheses Mixing concatenation order incorrectly
Caution

Always use parentheses when mathematical calculation is required before concatenation.


Why Concatenation Is Important

Concatenation helps you: Create readable output Display dynamic messages Combine data into meaningful sentences
Goal Achieved

You now understand how to combine text and variables using the + operator in Java.

Learn Java Concatenating Text and Variables | Java Course