String Concatenation in Java

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr
konkatenaciya_vertex-academy
1. What is concatenation and why you shouldn’t be afraid of it?

concatenation_vertex-academy

Concatenation is a synonym to “combination”. The word has Latin roots: “con” means “together”, “catena” stands for “chain”. It means “link together”.

2. How is the concatenation used in Java?

The answer is: it is used when working with strings. The string concatenation in Java basically means merging, “agglutination” of two strings into one string. Let’s see how it works on the following example:

Example No.1

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

Morning is good today

Morning is not good today, because it’s Monday

Comments:

In these two strings we declared 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 so to say “agglutinated” separate strings into one string by means of  the  + operator.

Example No.2

If you try to 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 we have two separate strings: “William” and “Porsche 918 Spyder”.

And in this string we concatenated the strings, i.e. we so to say “agglutinated” the separate strings into one string by means of the + operator.

Yeah, William bought a very beautiful car. Here is the beauty, eh:

porsche-918_vertex-academy

Why can’t we just say “merging”?

You could think “Why can’t we just say “coupling” or “merging”. I want to say, that 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 better for you to remember it right up front – it will be much easier to talk with your colleagues.

Secondly, the string concatenation can be carried out not only by means of the + operator, there are also several different ways to do it. For example, a commonly used method to merge strings in Java is called .concat() (which, obviously, stands for concatenation). And it will be of service to you in future. However, we will cover this topic a little bit later.

For now it is enough to learn the concatenation by means of the + operator.

Let’s summarize:

1. Concatenation is a synonym to “combination”

concatenation_vertex-academy

2. In Java there are several ways to merge the strings. At the time we know only about one of them: by means of the + operator.

 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 4 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--