字符串被定义为一个字符数组。字符数组和字符串之间的区别在于字符串以特殊字符n结尾。 由于数组是不可变的(不能增长),因此字符串也是不可变的。 每当对 String 进行更改时,都会创建一个全新的 String。 连接是端到端连接的过程。

concat()方法

String 类的 Java String concat()方法本身就存在于 java.util 包中,它将一个字符串连接到另一个字符串的末尾。 此方法返回一个字符串,其中包含传递给该方法的字符串的值,附加到字符串的末尾。

原理说明:

Input:  String 1   : abc
        String 2   : def
        String n-1 : ...
        String n   : xyz
Output: abcdef...xyz

示例代码:

// Java Program to Demonstrate Working of concat() method

// Main class
class Yiibai {

    // Main driver method
    public static void main(String args[]) {
        // Custom input string 1
        String s1 = "Yii";

        // Custom input string 2 is passed as in arguments
        // Here we are adding it to end of same string
        s1 = s1.concat("Bai ");

        String s2 = s1.concat(".com");

        // Printing the concatenated string
        System.out.println(s1);
        System.out.println(s2);
    }
}

运行结果:

YiiBai 
YiiBai .com

+运算符

+ 运算符用于在任一侧连接字符串,并且很可能将两个数字相加,从而为提供了在任一侧添加的灵活性。

例子:

// Java Program to Demonstrate Working of + Operator
// over Strings

// Main class
class Yiibai {

    // Main driver method
    public static void main(String args[]) {
        // Custom input strings
        String s1 = " Yiibai ";
        String s2 = " for Geeks ";

        // Now using + operator over string where
        // flexibility is provided to add on either side
        String s3 = s1 + s2;
        String s4 = s2 + s1;

        // Printing strings formed after
        // using + operator
        System.out.println(s3);
        System.out.println(s4);
    }
}

运行结果:

 Yiibai  for Geeks 
 for Geeks  Yiibai

虽然 Strings 类的 concat() 方法和 + 运算符都用于字符串的连接,但它们之间存在一些差异,如下所示:

因素1concat() 方法和 + 运算符采用的参数数量

  • concat()方法只接受字符串的一个参数并将其与其他字符串连接。
  • +运算符接受任意数量的参数并连接所有字符串。

示例代码

// Java Program to Illustrate concat() method
// vs + operator in Strings

// Main class
public class Yiibai {

    // Main driver method
    public static void main(String[] args) {
        // Custom input string
        String s = "Yiibai", t = "for", g = "geeks";

        // Printing combined string using + operator
        System.out.println(s + t + g);

        // Printing combined string using concat() method
        System.out.println(s.concat(t));
    }
}

运行结果:

Yiibaiforgeeks
Yiibaifor

因素2、:参数的类型

  • String concat()方法只接受字符串参数,如果参数中给出了任何其他类型,那么它将引发错误。
  • +运算符采用任何类型并转换为字符串类型,然后连接字符串。

因素3、:concat()方法引发 java.lang.NullPointer 异常

  • 当字符串与 null 连接时,concat() 方法抛出 NullPointer 异常
  • 当字符串与 null 连接时,+ 运算符没有引发任何异常。

示例:

// Java Program to Illustrate Raise of NullPointer Exception
// in case of concat() Method

// Main class
public class Yiibai {

    // Main driver method
    public static void main(String[] args)
    {

        // Input string 1
        String s = "Geeks";
        // Input string 2
        String r = null;

        // Combining above strings using + operator and
        // printing resultant string
        System.out.println(s + r);

        // Combining above strings using concat() method and
        // printing resultant string
        // It raises an NullPointer Exception
        System.out.println(s.concat(r));
    }
}

运行结果:

Yiibai tutorialsnull
Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.concat(String.java:2027)
    at Yiibai.main(Yiibai.java:22)
Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    ... ...

因素4、创建一个新的 String 对象

  • concat()方法连接两个字符串并返回一个新的字符串对象,只有字符串长度大于 0,否则返回相同的对象。
  • + 运算符每次都会创建一个新的 String 对象,而不考虑字符串的长度。

示例:

// Java Program to Illustrate Creation of New String object
// in concat() method and + Operator

// Main class
public class Yiibai {

    // Main driver method
    public static void main(String[] args) {

        // Input strings
        String s = "Geeks", g = "";

        // Using concat() method over strings
        String f = s.concat(g);

        // Checking if both strings are equal
        // Case 1
        if (f == s) // Identical strings
        {
            System.out.println("Both are same");
        } else // Non-identical strings
        {
            System.out.println("not same");
        }

        // Case 2
        String e = s + g;

        // Again checking if both strings are equal
        if (e == s) // Identical strings
        {
            System.out.println("Both are same");
        } else // Non-identical strings
        {
            System.out.println("not same");
        }
    }
}

运行结果:

Both are same
not same

因素5:性能

concat()方法比 + 运算符更好,因为它仅在字符串长度大于零 (0) 时创建一个新对象,但 + 运算符总是创建一个新字符串,而与字符串的长度无关。

结论:从上面的两个程序中,我们看到两者都以某种方式直接或间接地将两个字符串相加。 在 concat 方法中,必须在添加字符串时添加字符串,因为必须在参数中传递字符串,而在“+”运算符的其他情况下,它只是简单地将字符串相加,可能是数学,所以可以添加任何一方都没有限制。

下表描述了两种方法之间的主要区别:

比较基础 concat()方法 +运算符
参数数量 仅采用字符串的一个参数并将其与另一个字符串连接。 接受任意数量的参数并连接所有字符串
参数类型 仅接受字符串参数,如果参数中给出任何其他类型,则会引发错误。 接受任何类型并转换为字符串类型,然后连接字符串。
NullPointer Exception 当字符串与 null 连接时抛出 NullPointer 异常 当字符串与 null 连接时不会引发任何异常
创建新的 String 对象 连接两个字符串,并且仅当返回的字符串的长度大于连接的字符串之一的长度时才返回一个新的字符串对象,否则返回相同的对象。 无论字符串的长度如何,每次都创建一个新的字符串对象。
性能 concat()方法优于 + 运算符,因为它仅在字符串长度大于零(0)时创建一个新对象,因此它使用的内存量更少。 +运算符总是创建一个新字符串,而不管字符串的长度如何,因此它需要更多的内存。