Concatenation in Java
1. What is concatenation and why you should not be afraid of it
Concatenation is a synonym of “combination.” The word has Latin roots: “con” means “together” and “catena” stands for “chain.” It means “to link together”.
2. When is concatenation used in Java?
The answer: when you work with strings. String concatenation in Java basically means merging, or “agglutination,” of two strings into one string. Let’s see how it works in the following example:
Example No. 1
1 2 3 4 5 6 7 8 9 10 |
public class Test { public static void main(String[] args) { String morning1 ="is good today"; String morning2 ="is not good today, because it`s Monday"; System.out.println("Morning " + morning1); System.out.println("Morning " + morning2); } } |
If you run this code on your computer, you will see the following in the console:
Morning is good today
Morning is not good today, because it’s Monday
Comments:
1 2 |
String morning1 ="is good today"; String morning2 ="is not good today, because it`s Monday"; |
In these two strings, we declared the String variables and assigned values to them. So we have two separate strings: “is good today” and “is not good today, because it’s Monday.”
1 2 |
System.out.println("Morning " + morning1); System.out.println("Morning " + morning2); |
In these two strings, we concatenated two strings, i.e. we combined separate strings into one string with the help of the + operator.
Example No. 2
1 2 3 4 5 6 7 8 9 |
public class Test { public static void main(String[] args) { String name = "William"; String car = "Porsche 918 Spyder"; System.out.println(name + " bought " + car); } } |
If you run this code on your computer, you will see the following in the console:
William bought Porsche 918 Spyder
Comments:
1 2 |
String name = "William"; String car = "Porsche 918 Spyder"; |
In these two strings, we declared two String variables and assigned values to them. So now we have two separate strings: “William” and “Porsche 918 Spyder.”
1 |
System.out.println(name + " bought " + car); |
And in this string, we concatenated the strings, i.e. we combined the separate strings into one string with the help of the + operator.
Yeah, William bought a very beautiful car. Here she is:
Why can’t we just say “merging?”
You might be thinking, “Why can’t we just say 'coupling' or 'merging?'" Well, there are at least two reasons to remember the term “concatenation.”
Firstly, “concatenation” is a commonly-used term among programmers. That’s why it’s best for you to memorize. It will make it much easier to communicate with your colleagues.
Secondly, string concatenation can be carried out several different ways, not only by means of the + operator. For example, a commonly-used method to merge strings in Java is called .concat() ("concat" obviously standing for "concatenation"). And it will be very useful to you in the future. However, we will cover that topic a little later.
For now, it is enough to know how concatenation works with the help of the + operator.
Let’s summarize:
1. Concatenation is a synonym of “combination”
2. In Java, there are several ways to merge strings. So far, we know only one of them: the + operator.