java值传递string_关于java:按值传递(StringBuilder与String)
本問題已經有最佳答案,請猛點這里訪問。
我不明白為什么system.out.println(name)在不受方法的concat函數影響的情況下輸出sam,而system.out.println(name)在方法的append方法的結果下輸出sam4。為什么StringBuilder受到影響而不是String?通常,對對象引用調用方法會影響調用方,因此我不理解為什么字符串結果保持不變。提前謝謝
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
}
這里有很多答案,基本上都是一樣的。很難選擇一個進行投票:)
@幸運的是,你不需要只選擇一個;)
@阿諾德諾耶爾,正如馬克·羅特韋爾所說,海倫斯是做出艱難選擇的人;
因為當你打電話給speak(name);的時候,當你打電話給speak(name);的時候,內部會說話。
name = name.concat("4");
它創建了一個新對象,因為String是不可變的。當您更改原始字符串時,它將創建一個新對象,我同意您返回它,但您沒有捕獲它。
所以本質上你所做的是:
name(new) = name(original) + '4'; // but you should notice that both the names are different objects.
嘗試
String name ="Sam";
name = speak(name);
當然,現在我認為沒有必要解釋為什么它與StringBuilder一起工作,除非你不知道StringBuilder是可變的。
"它創建了一個新對象,因為字符串是不可變的。"在語言中沒有"可變"的概念。它返回一個新的對象,因為這就是該方法的文檔所要做的。它不會修改它所調用的對象,同樣,因為這就是該方法的文檔所要做的。
@很抱歉,先生,我肯定我沒有你知道的那么多,但是我想如果我說"它返回一個新對象,因為它的方法被記錄下來了",OP會不高興的。所以這就是為什么我想給出一些背后的理由。
但是我們只說類是"不可變的",因為它的所有方法都被記錄為不修改它。所以說方法不修改它是循環推理,因為它是不可變的。
@紐阿克特可能是"雞還是蛋":p
看看JavaDoc for String,你會發現
[...] String objects are immutable [...].
這意味著concat(String)并沒有改變String本身,而是構建了一個新的String。
另一方面,StringBuilders是可變的。通過調用append(CharSequence),對象本身就發生了變化。
好的,speak方法在做什么?
首先,
name.concat("4");
創建新對象,該對象等于name,與"4"連接。
所以,這條線
name = name.concat(4);
重新定義局部(用于speak方法)變量name。
然后返回對這個新值的引用
return name;
因此,方法中傳遞的原始變量不會被修改,但該方法返回修改后的值。
在test方法中,實際上修改變量而不修改引用(StringBuilder類是可變的,因此如果可以修改此類型,則是可變的)。
然后我們可以看到另一個問題出現了:為什么StringBuilder.append返回值,而這個值似乎是多余的。這個問題的答案在于對"構建器"模式的描述,這是實現修改方法的常用方法。參見維基百科的建設者模式。
在方法speak中,concat方法返回一個新字符串,它調用的原始對象不變(字符串是不可變的)。如文件所示:
If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
稱為name.concat("4")相當于name +"4"。
在您的test方法中,append方法修改StringBuilder的內容。如文件所示:
The principal operations on a StringBuilder are the append and insert 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. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
在您的主方法中,name和names仍然是方法調用之前的同一對象,但name的內容不變,因為字符串是不可變的,而names的內容已更改。
如果您使用了這兩個方法的返回值,那么您將得到預期的結果。
由于String是不可變的,因此String#concat不修改原來的字符串實例,它只返回一個新的String,而原來的則保持不變,而StringBuilder是可變的,其變化反映在作為參數傳遞的StringBuilder實例中。
在Java中,EDCOX1的1Ω是不可變的。一旦對名稱調用concat方法。將創建一個新字符串,當您在System.out.println(name)中使用舊的引用時。如果要使用修改后的字符串,應顯式返回引用。雖然StringBuilder是可變的,它總是返回相同的引用。
當調用speak(name)時,它計算新值,但丟棄它。
如果你把它換成
name = speak(name);
結果就是你所期望的。
使用StringBuilder時,您傳遞的對象是可變的:所以
names.append(names);
更改當前對象的狀態(它還返回對同一對象的引用,這只是方便您編寫代碼,如names.append(...).append(...)等)。因此,在StringBuilder的情況下,調用方法時所引用的對象實際上已經更改,因此您可以看到更改。
您沒有回答這個問題:為什么它與StringBuilder一起工作?
是的,是真的。參見更新。
首先,EDCOX1×0是Java中不可變的類。不可變類只是不能修改其實例的類。實例中的所有信息在創建實例時初始化,不能修改。
第二,Java參數是通過值發送的,而不是通過引用發送的。
在你的"測試"方法中,你不需要names = names.append("4"),相反,names.append("4")就足夠了。
如果您檢查Java對象的字符串對象,您將看到其中的大多數方法,包括CONTAT,將生成一個新的字符串。
因此,要讓輸出sam4也用于字符串,您需要在主方法中使用這個name = speak(name)。
弦
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
.
有關詳細信息,請檢查此
結論:您不需要再將該值重新分配給StringBuilder,因為它已經是一個引用測試方法應為
public static void test(StringBuilder names) {
names.append("4");
}
但說話應該是
String name ="Sam";
name = ?speak(name);
這不是被問到的問題。
好的,我更新了答案
總結
以上是生活随笔為你收集整理的java值传递string_关于java:按值传递(StringBuilder与String)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQLite学习手册(开篇)
- 下一篇: SQLite学习手册(C/C++接口简介