How do you concatenate strings in Java?

Artículo revisado y aprobado por nuestro equipo editorial, siguiendo los criterios de redacción y edición de YuBrain.

In most programming languages, Java included, concatenate refers to the operation of joining two or more character strings into a single new longer string . This can be done in different ways depending on the programming language being used, and even within the same programming language, there are often several ways to concatenate strings.

In the case of the Java programming language, strings fall under one of the most important data types called String . In Java, Strings are objects, and there are two different ways to concatenate them: one is using the addition or addition operator (+), and the other is using the concat() method of the String class .

Let’s see how both methods work:

Concatenate strings in Java using the + operator

This is one of the easiest and most widely used ways to concatenate strings in Java. This form of concatenation simply consists of interposing the + operator between the strings to be concatenated.

For example, if we write:

“This is how you concatenate strings” + “ in Java.”

The result will be: This is how strings are concatenated in Java.

The + operator allows you to concatenate multiple consecutive strings.

One of the advantages of concatenating using the + operator is that it allows you to concatenate multiple strings, one after another. For example, if we wanted to combine “This is how strings are concatenated” with ” in Java.” And “It’s very easy!”, then it would only be necessary to interpose the operator + between each pair of expressions:

“This is how you concatenate strings” + “ in Java.” + “It’s so easy!”

This will result in a string that says: This is how to concatenate strings in Java. Too easy!

The + operator can be used within the println() statement

In case you want to print the result of concatenating several character strings, this is as simple as using the + operator inside the println() statement .

The + operator allows you to combine different objects in the form of a string.

An important feature of the + operator is that it accepts predefined variables as arguments, as well as numbers and string literals (quoted expressions such as “Hello world”).

The operator accomplishes this by automatically calling the toString() method when acting on non-string objects, transforming them into strings before concatenating them to the rest. In this way, it can be used to generate messages in the form of strings from other objects in the programming environment.

For example, the following code defines a few different objects and then uses the + operator to concatenate them into a single String to print to the screen:

String message 1 = “This is how strings are concatenated ”;

String message 2 = “different objects.”;

Int bnum = 3;

System.out.println (message1 + “from “ + numob + message2);

Running this code will print the following message:

This is how strings are concatenated from 3 different objects.

Concatenate strings in Java using the “concat()” method of the String class

In Java, the String class of objects has the concat() method that performs a function similar to that of the + operator, that is, it allows one string of characters to be concatenated with another. However, it has some fundamental differences with said operator.

This method works by concatenating the characters of a second String that it takes as argument to the original String . For example:

String message 1 = “This is how strings are concatenated ”;    // this createsString message1

String message 2 = “using the concat () method.”;    // this createsString message2

message1 = message1.concat(message2);                // this concatenates message1 with message2

System.out.println (message1)

The result of this code is that it will be printed on the screen:

This is how you concatenate strings using the concat () method.

Which way of concatenating is preferable in Java?

The natural question when there is more than one way to do things is: “Which is better?”

Generally speaking, the + operator is more versatile than the concat () method for several reasons:

  • It allows you to concatenate an unlimited number of objects while the concat method only allows you to concatenate two at a time.
  • It allows you to combine different classes of objects into a single string by automatically calling the toString () method when a variable or object is not a string. In contrast, concat can only be called on an object that is a String , and only takes another String as a parameter .

For these two reasons, the + operator is used much more frequently than the concat method . However, there is a situation where the concat method can be beneficial. Unlike the + operator, the concat method does not transform its argument to a string. For this reason, if you try to concatenate an object that doesn’t exist, the application will throw an error ( NullPointerException ) and it won’t go unnoticed, as it would if you used the + operator instead.

Israel Parada (Licentiate,Professor ULA)
Israel Parada (Licentiate,Professor ULA)
(Licenciado en Química) - AUTOR. Profesor universitario de Química. Divulgador científico.

Artículos relacionados