Concatenation in Java

konkatenaciya_vertex-academy
1. What is concatenation and why should you not be afraid of it?

concatenation_vertex-academy

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

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:

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.”

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

If you run this code on your computer, you will see the following in the console:

William bought Porsche 918 Spyder

Comments:

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.”

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:

porsche-918_vertex-academy

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”

concatenation_vertex-academy

2. In Java, there are several ways to merge strings. So far, we know only one of them: the + operator.

 

FacebookFacebook

FacebookFacebook