JavaString Concatenation
String Concatenation
The+ operator can be used between strings to combine them. This is calledconcatenation:
Example
String firstName = "John";String lastName = "Doe";System.out.println(firstName + " " + lastName);Note that we have added an empty text (" ") to create a space between firstName and lastName on print.
Concatenation in Sentences
You can use string concatenation to build sentences with both text and variables:
Example
String name = "John";int age = 25;System.out.println("My name is " + name + " and I am " + age + " years old.");Result:
My name is John and I am 25 years old.The concat() Method
You can also use theconcat() method to concatenate strings:
Example
String firstName = "John ";String lastName = "Doe";System.out.println(firstName.concat(lastName));You can also join more than two strings by chainingconcat() calls:
Example
String a = "Java ";String b = "is ";String c = "fun!";String result = a.concat(b).concat(c);System.out.println(result);Note: While you can useconcat() to join multiple strings, most developers prefer the+ operator because it is shorter and easier to read.

