Pass-by-value (StringBuilder vs String)
我不明白为什么system.out.println(name)在不受方法的concat函数影响的情况下输出sam,而system.out.println(name)在方法的append方法的结果下输出sam4。为什么StringBuilder受到影响而不是String?通常,对对象引用调用方法会影响调用方,因此我不理解为什么字符串结果保持不变。提前谢谢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static String speak(String name) { name = name.concat("4"); return name; } public static StringBuilder test(StringBuilder names) { names = names.append("4"); return names; } public static void main(String[] args) { String name ="Sam"; speak(name); System.out.println(name); //Sam StringBuilder names = new StringBuilder("Sam"); test(names); System.out.println(names); //Sam4 } |
因为当你打电话给
1 | name = name.concat("4"); |
它创建了一个新对象,因为
所以本质上你所做的是:
1 | name(new) = name(original) + '4'; // but you should notice that both the names are different objects. |
尝试
1 2 |
当然,现在我认为没有必要解释为什么它与
看看JavaDoc for
[...] String objects are immutable [...].
这意味着
另一方面,
好的,
首先,
1 | name.concat("4"); |
创建新对象,该对象等于
所以,这条线
1 | name = name.concat(4); |
重新定义局部(用于
然后返回对这个新值的引用
1 | return name; |
因此,方法中传递的原始变量不会被修改,但该方法返回修改后的值。
在
然后我们可以看到另一个问题出现了:为什么
在方法
If the length of the argument string is
0 , then thisString object is returned. Otherwise, aString object is returned that represents a character sequence that is the concatenation of the character sequence represented by thisString object and the character sequence represented by the argument string.
称为
在您的
The principal operations on a
StringBuilder are theappend andinsert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. Theappend method always adds these characters at the end of the builder; theinsert method adds the characters at a specified point.
在您的主方法中,
如果您使用了这两个方法的返回值,那么您将得到预期的结果。
由于
在Java中,EDCOX1的1Ω是不可变的。一旦对名称调用
当调用
如果你把它换成
1 | name = speak(name); |
结果就是你所期望的。
使用
1 | names.append(names); |
更改当前对象的状态(它还返回对同一对象的引用,这只是方便您编写代码,如
首先,EDCOX1×0是Java中不可变的类。不可变类只是不能修改其实例的类。实例中的所有信息在创建实例时初始化,不能修改。
第二,Java参数是通过值发送的,而不是通过引用发送的。
在你的"测试"方法中,你不需要
如果您检查Java对象的字符串对象,您将看到其中的大多数方法,包括CONTAT,将生成一个新的字符串。
因此,要让输出sam4也用于字符串,您需要在主方法中使用这个
弦
String is immutable ( once created can not be changed )object . The
object created as a String is stored in the Constant String Pool .
Every immutable object in Java is thread safe ,that implies String is
also thread safe . String can not be used by two threads
simultaneously. String once assigned can not be changed.String demo =" hello" ; // The above object is stored in constant
string pool and its value can not be modified.demo="Bye" ; //new"Bye" string is created in constant pool and
referenced by the demo variable //"hello" string still
exists in string constant pool and its value is not overrided but we
lost reference to the "hello"string
字符串拼接
StringBuilder is same as the StringBuffer , that is it stores the
object in heap and it can also be modified . The main difference
between the StringBuffer and StringBuilder is that StringBuilder is
also not thread safe. StringBuilder is fast as it is not thread safe
.
有关详细信息,请检查此
结论:您不需要再将该值重新分配给
1 2 3 | public static void test(StringBuilder names) { names.append("4"); } |
但说话应该是
1 2 |