Home » Java programming language

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.
Submitted by Preeti Jain, on July 11, 2019

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:

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:

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.

'+' 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:

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:

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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.