📜  Javaconcat() 和 +运算符的区别

📅  最后修改于: 2021-09-11 03:35:34             🧑  作者: Mango

concat() 方法

Java String concat() 方法将一个字符串连接到另一个字符串的末尾。此方法返回一个字符串,其中包含传递给该方法的字符串值,并附加到字符串的末尾。

例子:

// 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);
    }
}

输出:

Gfg! is the best.

+运算符

+运算符用于连接任一侧的字符串。

例子:

// 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);
    }
}

输出:

Gfg! is the best.

虽然 concat() 和 +运算符都用于字符串的连接,但它们之间还是有一些区别的:

  1. concat() 方法和 +运算符采用的参数数量:
    • concat()方法只接受字符串 的一个参数并将其与其他字符串。
    • +运算符接受任意数量的参数并连接所有字符串。
    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));
        }
    }
    
    输出:
    Geeksforgeeks
    Geeksfor
    
  2. 参数类型:
    • strong>concat() 方法只接受字符串参数,如果参数中有任何其他类型,则会引发错误。
    • +运算符接受任何类型并转换为字符串类型,然后连接字符串。
  3. concat() 方法引发Java.lang.NullPointer 异常
    • 当字符串与 null 连接时, concat() 方法抛出 NullPointer 异常
    • +运算符在字符串与 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));
        }
    }
    
    输出:
    Geeksnull
    Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.concat(String.java:2027)
        at GFG.main(GFG.java:7)
    
  4. 创建一个新的 String 对象。
    • concat() 方法需要连接两个字符串并返回新的字符串对象,只有字符串长度大于 0,否则返回相同的对象。
    • +运算符每次都会创建一个新的字符串对象,而不管字符串的长度。
    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");
        }
    }
    
    输出:
    Both are same
    not same
    
  5. 表现:
    concat()方法+运算符更好,因为它创建一个新对象,只有当字符串长度大于零(0)但+运算符总是不论字符串的长度的创建一个新的字符串。

差异表:

Points concat() method + operator
Definition A concat() method is method to combine two strings . + operator used to concatenate any number of strings.
Number of arguments In 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 arguments concat() 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 string concat() 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 Exception In concat() method raises NullPointer Exception when string is concatenated with null . + operator concatenates string with without any error.
Performance concat() 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.