Java: How to Concatenate Two String in Java

Java has made it very possible for 2 strings to be combined and that process is called concatenation. we will discuss the two very simple ways of concatenating in Java.

This article will be beneficial to readers who are interested in Java and will love to know how concatenating is done, so let's get started.

To follow along with this article, you will need the following;

  • Java IDE either Eclipse, Intelli J.

  • Basic knowledge of Java programming language.

Let's get started!

What is Concatenating in Java?

First thing first, let's define what string is, string is a sequence of character(s), it can be a word, phrase, or sentence. Now concatenate string means adding two different characters together, which are a series of words or sentences.

Concatenating with + operation

If we have two strings, "Good" and "Morning". We can concatenate them to create a new string, "Good Morning" Let's dive into our IDE for easy understanding,

package philip;

public class string {

    public static void main(String[] args) {
        // Concatenating string using + operator.

        String word = "Good";
        String word2 = "Morning";


    }

}

Let me explain: As shown above, we have two strings of word that contains the character Good and word2 which contains the character Morning. To concatenate them, using the + operation, we introduce the + to word and word2 and print the command using System.out.println();

package philip;

public class string {

    public static void main(String[] args) {
        // Concatenating string using + operator.

        String word = "Good";
        String word2 = "Morning";
System.out.println( word + word2);
Output = GoodMorining
//let's put a space between the output.
System.out.println(word + " " + word2);
output = Good Morning
    }

}

As shown above, I know, you may be confused with " ". That was introduced to create space between Good + Morning.

Concatenating Strings with Numbers

Previously, we've learned how to concatenate two strings, now, let's concatenate strings and numbers.

It's important to note that strings in programming represent words or characters while numerals represent numbers. So when concatenating string and numbers together, the number is first changed to a string before concatenating can take place.

Let's dive into an example;

package philip;

public class Concatenate {

    public static void main(String[] args) {
        // Concatenating string and numbers using + operator.

        String word = "my favourite number is ";
        String word2 = "50";
System.out.println(word + " " + word2);

    }

}

Note the number 50 I stored in " "which automatically converts it to a string.

When + operator is used with strings, it acts as a concatenating operator When + operator is used with numbers it acts as an addition operator

Concatenating by concat() method

This method allows you to join two words together without having to change anything. It joins a specified string to the end of a current string. This is already a built-in method in the string class.

Let's check out this cool example using Eclipse IDE

package philip;

public class Concatenate {

    public static void main(String[] args) {
        // Concatenating string using concat() method.

        String word = "my name is ";
        String word2 = "Philip Essien";
        String result = word.concat(word2);
System.out.println(word + " " + word2);

    }

}

The above example concatenates two String word and word2 using Concat() method and we use System.out.println to display the value of the result

Conclusion

Concatenating is important in Java, it can be used to create a simple new string. However, if you using concatenating string and numbers it is important to understand data types to avoid errors while coding.

in Java the concat() method is the most convenient, as it allows you to join both strings and numbers without modifying the original string. I hope you had fun reading this as much as I did.