Skip to content
Related Articles

Related Articles

Improve Article

Difference between concat() and + operator in Java

  • Difficulty Level : Basic
  • Last Updated : 05 Apr, 2019

concat() Method

The Java String concat() method concatenates one string to the end of another string. This method returns a string with the value of the string passed into the method, appended to the end of the string.

Example:




// Java program to demonstrate
// working of concat() method
  
class Gfg {
    public static void main(String args[])
    {
        String s = "Gfg";
        s = s.concat("! is the best.");
        System.out.println(s);
    }
}

Output:

Gfg! is the best.

+ operator



+ operator is used to concatenate strings on either side.

Example:




// Java program to demonstrate
// working of concat() method
  
class Gfg {
    public static void main(String args[])
    {
        String s1 = "Gfg";
        String s2 = "! is the best";
  
        String s3 = s1 + s2;
  
        System.out.println(s3);
    }
}

Output:

Gfg! is the best.

Although concat() and + operator are both used for concatenation of strings, but there are some differences between them:

  1. Number of arguments the concat() method and + operator takes:
    • concat() method takes only one argument of string and concat it with other string.
    • + operator takes any number of arguments and concatenates all the strings.




    public class GFG {
        public static void main(String[] args)
        {
            String s = "Geeks", t = "for", g = "geeks";
      
            System.out.println(s + t + g);
            System.out.println(s.concat(t));
        }
    }
    Output:
    Geeksforgeeks
    Geeksfor
    
  2. Type of arguments :
    • strong>concat() method takes only string arguments, if there is any other type is given in arguments then it will raise an error.
    • + operator takes any type and converts to string type and then concatenates the strings.
  3. concat() method raises java.lang.NullPointer Exception
    • concat() method throws NullPointer Exception when string is concatenated with null
    • + operator did not raise any Exception when the string is concatenated with null.




    public class GFG {
        public static void main(String[] args)
        {
            String s = "Geeks";
            String r = null;
            System.out.println(s + r);
      
            // It raises an NullPointer Exception
            System.out.println(s.concat(r));
        }
    }
    Output:
    
    Geeksnull
    Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.concat(String.java:2027)
        at GFG.main(GFG.java:7)
    
  4. Creates a new String object.
    • concat() method takes concatenates two strings and return new string object only string length is greater than 0, otherwise it returns same object.
    • + operator creates a new string object every time irrespective of length of string.




    public class GFG {
        public static void main(String[] args)
        {
      
            String s = "Geeks", g = "";
            String f = s.concat(g);
            if (f == s)
                System.out.println("Both are same");
            else
                System.out.println("not same");
            String e = s + g;
            if (e == s)
                System.out.println("Both are same");
            else
                System.out.println("not same");
        }
    }
    Output:
    Both are same
    not same
    
  5. Performance:
    concat() method is better than + operator because it creates a new object only when the string length is greater than zero(0) but + operator always a creates a new string irrespective of length of string.

Difference table:

Pointsconcat() method+ operator
DefinitionA concat() method is method to combine two strings .+ operator used to concatenate any number of strings.
Number of argumentsIn concat() method, takes only one argument of string and concatenate it with another string.In + operatortakes any number of arguments and combines all strings.
Type of argumentsconcat() method takes arguments of string type only.+ operator takes any type of argument and converts it to string type and then combine them.
Creates new stringconcat() takes concatenates two strings and return new string object only string length is greater than 0, otherwise it returns same object..+ operatorcreates a new string object every time irrespective of length of string.
NullPointer ExceptionIn concat() method raises NullPointer Exception when string is concatenated with null .+ operator concatenates string with without any error.
Performanceconcat() method is better than + operator because it creates a new object only when the string length is greater than zero(0), so it uses less amount of memory.+ operator always a creates a new string irrespective of length of string therefore it takes more memory.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :