Java 使用字符串或+中的concat操作进行连接;操作人员

Java 使用字符串或+中的concat操作进行连接;操作人员,java,string,Java,String,我知道字符串的不变性,但我不知道这与连接有什么关系。以下代码的输出是什么以及为什么: String str1="my string"; System.out.println(str1); System.out.println(str1.concat(" gets appended")); System.out.println(str1+" getting updated"); 原因,根据我的理解,str1因其不变性而不应更改。因此,对于所有情况,输出都应该是我的字符串 谢谢大家的澄清。但是,

我知道字符串的不变性,但我不知道这与连接有什么关系。以下代码的输出是什么以及为什么:

String str1="my string";
System.out.println(str1);
System.out.println(str1.concat(" gets appended")); 
System.out.println(str1+" getting updated");
原因,根据我的理解,str1因其不变性而不应更改。因此,对于所有情况,输出都应该是我的字符串

谢谢大家的澄清。但是,请查看下面的代码及其输出

代码:

    String str1= new String("First string");
    String str2= "Second string";

    System.out.println("String1: "+str1);
    System.out.println("String2: "+str2);

    str1 = str1.concat(" is now appended");             
    System.out.println(str1);

    str2 = str2.concat(" is now appended");             
    System.out.println(str2);

    str1 = str1+" is getting added";
    System.out.println(str1);

    str2 = str2+" is getting added";
    System.out.println(str2);
    String1: First string
    String2: Second string
    First string is now appended
    Second string is now appended
    First string is now appended is getting added
    Second string is now appended is getting added
输出:

    String str1= new String("First string");
    String str2= "Second string";

    System.out.println("String1: "+str1);
    System.out.println("String2: "+str2);

    str1 = str1.concat(" is now appended");             
    System.out.println(str1);

    str2 = str2.concat(" is now appended");             
    System.out.println(str2);

    str1 = str1+" is getting added";
    System.out.println(str1);

    str2 = str2+" is getting added";
    System.out.println(str2);
    String1: First string
    String2: Second string
    First string is now appended
    Second string is now appended
    First string is now appended is getting added
    Second string is now appended is getting added
有人能告诉我为什么要修改吗?我知道要修改字符串,我们需要使用StringBuilder类或StringBuffer类。但是,这里没有使用这些类,即使我使用了字符串,它也会被修改。 它是在内部使用StringBuilder类还是StringBuffer类?我使用的是jdk版本8


提前感谢。

您在任何时候都不会更改str1的参考值。在您的示例中,您正在创建两个新的
String
实例,它们的值为
str1
,以及附加在那里的任何其他值。

new
String
将在调用
str1.concat(“获取附加”)
str1+“获取更新”
时返回,它不影响原始的str1

System.out.println(str1); // my string
System.out.println(str1.concat(" gets appended")); // my string gets appended
System.out.println(str1 + " getting updated"); // my string getting updated
System.out.println(str1); // my string

更新:

您需要了解变量对象之间的差异。
str1
是一个变量,它指的是字符串对象第一个字符串。当您调用
str1=str1+“正在添加”时
,实际上是让变量
str1
引用新字符串现在添加的第一个字符串

试试这个:

String string = "original string";
String string_holder = string;
string = string + " contact";
System.out.println(string); // original string contact
System.out.println(string_holder); // original string, the original string is not affected

原始字符串没有更改,都会创建一个新字符串并打印出来。

调用str.concat1(str2)时字符串没有更改的原因是该方法是如何写入String类的。假设我们制作自己的小示例类:

public class MutStr {
    private String str;
    public MutStr(String str) {
        this.str = str;
    }
    public String addString(String input) {
        str = str + input;             //changes the object itself
        return str;                    //returns the changed object
    }
}
这个类不是不可变的,因为在运行addString()方法时,我们会更改对象本身。但是,当我们更改方法时,可以使对象不可变:

    public String addString(String input) {
        String output = str + input;   //create a new object
        return output;                 //returns the new object
    }
两种方法返回相同的东西;新字符串添加到旧字符串中,但在内部,第一个方法更改对象本身,而第二个方法不更改


关键的认识是,方法返回的内容(println命令使用的内容)与对象本身不同。

输出将是什么-尝试时发生了什么?只是一个旁注,在Java中处理字符串连接的最有效方法是使用
StringBuilder
StringBuffer
。您展示的所有示例在性能方面都可能很差。如果
x
y
是字符串,
x.concat(y)
x+y
也会做同样的事情。@TimBiegeleisen实际上不是这样。+操作符已经尽可能地在内部使用StringBuilder,而concat()实际上做得更好,只要您只需要执行一次串联。当需要使用循环重复连接到字符串时,StringBuilder非常有用。在这种情况下,编译器无法有效地使用StringBuilders实现+运算符,因此程序员需要这样做。在其他情况下,相同的差异。谢谢你的回答。我明白你的解释了。现在我已经更新了我的问题,你能再看一次吗?我建议添加第四行再次打印
str1
,这将表明它仍然具有值
“我的字符串”
@Lino我刚刚同时添加了它:)@Sun谢谢你的解释。正如您所解释的,我已经尝试了一段新代码,但它似乎不是一成不变的。你能看看我更新的问题吗?@Makato我已经更新了我的问题。你能再看一眼吗?@Mani:你的问题变了。以前,您没有做任何作业或更改参考点。现在,你是。