Java String concat() method

concat() method of String class helps append the specified string to the end of a string. In this post, we will look at the concat() method in detail.

  • Method declaration – public String concat(String str).
  • What does it do? It accepts a string in the function’s argument and appends it at the end of the string calling the function.
  • What does it return? It returns a String object after appending the specified string.

Code example

The below code illustrates the use of the concat() method.

public class Codekru {

	public static void main(String[] args) {

		String s1 = "hello";
		System.out.println("Before using concat() method, s1 = " + s1);
		
		s1 = s1.concat(" codekru");
		System.out.println("After using concat() method, s1 = " + s1);

	}
}

Output –

Before using concat() method, s1 = hello
After using concat() method, s1 = hello codekru

Facts about concat() method

It seems easy to use, but one vital point is that when we use the concat() function, it only returns the same string object reference if the string passed in the function’s argument is empty. Otherwise, it will return a new String object, the combination of both strings. The below code might clarify things further.

public class Codekru {

	public static void main(String[] args) {

		String s1 = "hello";
		String s2 = "";
		String s3 = " codekru";
		String s4 = s1.concat(s2);
		String s5 = s1.concat(s3);

		if (s1 == s4) {
			System.out.println("s1 and s4 both referring to the same object"); // this will be printed
		} else {
			System.out.println("s1 and s4 referring to the different objects");
		}

		if (s1 == s5) {
			System.out.println("s1 and s5 both referring to the same object");
		} else {
			System.out.println("s1 and s5 referring to the different objects"); // this will be printed
		}
	}
}

Output –

s1 and s4 both referring to the same object
s1 and s5 referring to the different objects

You see that both s1 ad s4 are pointing to the same object, while s1 and s5 are pointing to different objects. So, every time you use concat() method of the String class, a new object is created if the appended string is not empty.

String concat method

Below is the implementation of the concat() method, where you can see that it returns the same object if the string passed in the argument is empty.

    public String concat(String str) {
        if (str.isEmpty()) {
            return this;
        }
        return StringConcatHelper.simpleConcat(this, str);
    }

Now, what if we passed the null value in the argument?

Well, let’s try to do it.

public class Codekru {

	public static void main(String[] args) {

		String s1 = "hello";
		s1 = s1.concat(null);
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "str" is null
	at java.base/java.lang.String.concat(String.java:1966)
	at Test.Codekru.main(Codekru.java:8)

It gave us NullPointerException, so we don’t recommend doing it 😛

And what if we used the concat() function on a null string?

Again, we will get a NullPointerException.

public class Codekru {

	public static void main(String[] args) {

		String str = null;
		str = str.concat("");
	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.concat(String)" because "str" is null

If you want to learn more about Strings and its methods, we recommend you read this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.