Differences between concat() method and plus (+) operator in Java

Compare concate() v/s + in Java: Here, we are going to learn differences about the concat() method and plus(+) operator in java programming language. By Preeti Jain Last updated : February 02, 2024

concat() method

  • concat() is a method used for concatenation of strings.
  • We pass only one string argument in concat() and concat it with other string.

Example: Concatenating two strings using contact() method

public class PassingArgument {
    public static void main(String[] args) {
        String str = "Java", lang = "Language";
        System.out.println(str.concat(lang));
    }
}

Output

D:\Programs>javac PassingArgument.java

D:\Programs>java PassingArgument
JavaLanguage
  • We should remember one thing at the time of passing the argument in concat() method. We can only pass a string and if we pass any other type of argument then we will get an error.
  • concat() method throws NullPointer Exception when a string is concatenated with 'null'.

Example: Concatenating string with null using contact() method

public class ConcatNull {
    public static void main(String[] args) {
        String str1 = "We will get an exception string concatenate with null in case of concat()";
        String str2 = null;
        // It raises an NullPointer Exception 
        System.out.println(str1.concat(str2));
    }
}

Output

D:\Programs>javac ConcatNull.java

D:\Programs>java ConcatNull
Exception in thread "main" java.lang.NullPointerException
       	 at java.base/java.lang.String.concat(String.java:1936)
        	at ConcatNull.main(ConcatNull.java:7)

The performance of concat() is high as compare to '+' because it generates a new object when string length is greater than 0.

Plus (+) operator

  • '+' is an operator used for concatenation of strings.
  • We can take any number of strings argument with '+' and merge it with all other strings.

Example: Concatenating two strings using plus (+) operator

public class PassingArgument {
    public static void main(String[] args) {
        String str = "Java", lang = "Language";
        System.out.println(str + lang);
    }
}

Output

D:\Programs>javac PassingArgument.java

D:\Programs>java PassingArgument
JavaLanguage
  • We don't need to remember anything at the time of passing argument in '+' operator. We can pass any type of argument if we pass other types of argument then we will not get any error.
  • '+' operator doesn't raise an exception when a string is concatenated with 'null'.

Example: Concatenating string with null using plus (+) operator

public class ConcatNull {
    public static void main(String[] args) {
        String str1 = "We will not get any exception when string concatenate with null in case of '+'";
        String str2 = null;
        // It will not raises any NullPointer Exception
        System.out.println(str1 + str2);
    }
}

Output

D:\Programs>javac ConcatNull.java

D:\Programs>java ConcatNull
We will not get any exception when string concatenate with null in case of '+'null

The performance of '+' operator is low as compare to concat() because it always generates new object whether string length is greater than 0 or less than 0.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.