String concatenation is the process where we add 2 strings to make up a new string. This can be done using the addition operator ( + ).
For instance :
String firstName = “Lahiru”;
String lastName = “Hashan”;
String name = firstName + lastName;
check out the output :
Output : LahiruHashan
No space because there is no space given.
Check out these examples :
Playing around with numbers :
Now identify the new line and its output. Check what has happened.
Now the answer has pop up without the brackets. As you can see the execution is happening from left to right. Therefore here the addition of 2 floats takes place first and then addition to the string takes place. This is an important aspect of Java.
There is a special method in String class to add strings, named “concat” method. We will be discussing in the next tutorial along with the other String methods.
See you next time.