Difference between concat() and + operator in Java
In this post, we will explain the difference between the concat() method and the + operator in Java.
Java provides two ways to concatenate strings: the concat() method and the + operator. Both are used to join two or more strings and return a new string as a result. However, they have some differences and trade-offs that we must understand.
1. Overview of the concat() method
The concat() method is a method of the String class that concatenates one string to the end of another string. This method returns a new string object with the value of the string passed as an argument, appended to the end of the string on which the method is called. For example, to concatenate two strings "Hello" and "World", we can write:
|
1 2 3 4 5 6 7 8 |
class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; String s = s1.concat(s2); System.out.println(s); // HelloWorld } } |
The concat() method can only take one argument of type String, and it throws a NullPointerException if the argument is null. For example:
|
1 2 3 4 5 6 7 8 |
class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = null; String s = s1.concat(s2); // NullPointerException System.out.println(s); } } |
The concat() method does not modify the original strings, but creates a new string object as a result. Therefore, it does not affect the memory usage or performance of the program significantly. However, it also means that we need to assign the result of the concat() method to a new variable or use it directly, otherwise it will be lost. For example:
|
1 2 3 4 5 6 7 8 |
class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; s1.concat(s2); // No effect System.out.println(s1); // "Hello" } } |
The concat() method can only concatenate two strings at a time. To concatenate more than two strings, we need to use multiple concat() methods or use parentheses to group them. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; String s3 = "!"; String s4 = s1.concat(s2).concat(s3); System.out.println(s4); // HelloWorld! String s5 = s1.concat(s2.concat(s3)); System.out.println(s5); // HelloWorld! String s6 = s1.concat((s2.concat(s3))); System.out.println(s6); // HelloWorld! } } |
2. Overview of the + operator
The + operator is a built-in operator in Java that can be used for various purposes, such as arithmetic addition, string concatenation, or unary plus. When used with strings, the + operator concatenates the strings on either side and returns a new string object as a result. For example, to concatenate two strings "Hello" and "World", we can write:
|
1 2 3 4 5 6 7 8 |
class Main { public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; String s = s1 + s2; System.out.println(s); // HelloWorld } } |
The + operator can take any number of arguments and concatenate them all. It can also take any type of arguments and convert them to strings before concatenating them. If an object is passed to the + operator, it gets converted to a string by the toString() method of that object. If a null reference variable is passed to the + operator, it gets converted to string "null". For example:
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { String s = "Hello"; int n = 42; boolean b = true; char c = '!'; String str = s + n + b + c; System.out.println(str); // Hello42true! } } |
Note that all primitive types (int, char, double, or float) gets converted to a string by calling the corresponding conversion method of the associated Wrapper class (Integer, Character, Double, Float). Note that the compiler may optimize the creation of a wrapper object by converting directly from a primitive type to a string.
To increase repeated string concatenation performance, a Java compiler uses the StringBuilder class to reduce the total number of intermediate string objects. Therefore, String a = a + b + c; is the equivalent of String a = new StringBuilder(a).append(b).append(c).toString();
3. Differences between concat() method and + operator in Java
The concat() method and the + operator are both used to concatenate strings in Java, but they have some differences and trade-offs that we must understand:
- The
concat()method is a method of theStringclass, while the+operator is a built-in operator in Java. - The
concat()method can only take one argument of type String, while the+operator can take any number of arguments of any type. - The
concat()method throws aNullPointerExceptionif the argument is null, while the+operator does not throw any exception if the argument is null. - The
concat()method can only concatenate two strings at a time, while the+operator can concatenate any number of strings at a time. - The
concat()method internally uses the superfastSystem.arraycopy()method for string concatenation, while a Java compiler may use theStringBufferclass for concatenating multiple strings together with+operator.
4. What to use and when?
As we have seen, the concat() method and the + operator are both useful methods to concatenate strings in Java. Here are some general recommendations on choosing between them:
- If you need to concatenate multiple strings of any type, we should use the
+operator. This method will allow us to concatenate any number of arguments and convert them to strings before concatenating them. However, we should also be aware of the memory usage and performance impact of this method, especially if we use it repeatedly or with large strings. - If you need to concatenate multiple strings of type String, we should use the
concat()method. This method will create a new string object only if necessary and avoid unnecessary memory allocation. However, we should also be careful with null strings when using this method.
In general, we can use the + operator if the total number of operations is less and the StringBuilder class when the total number of operations is more. That’s all about the differences between the concat() method and the + operator in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)