日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java值传递string_关于java:按值传递(StringBuilder与String)

發(fā)布時(shí)間:2025/3/12 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java值传递string_关于java:按值传递(StringBuilder与String) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本問(wèn)題已經(jīng)有最佳答案,請(qǐng)猛點(diǎn)這里訪問(wèn)。

我不明白為什么system.out.println(name)在不受方法的concat函數(shù)影響的情況下輸出sam,而system.out.println(name)在方法的append方法的結(jié)果下輸出sam4。為什么StringBuilder受到影響而不是String?通常,對(duì)對(duì)象引用調(diào)用方法會(huì)影響調(diào)用方,因此我不理解為什么字符串結(jié)果保持不變。提前謝謝

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

}

這里有很多答案,基本上都是一樣的。很難選擇一個(gè)進(jìn)行投票:)

@幸運(yùn)的是,你不需要只選擇一個(gè);)

@阿諾德諾耶爾,正如馬克·羅特韋爾所說(shuō),海倫斯是做出艱難選擇的人;

因?yàn)楫?dāng)你打電話給speak(name);的時(shí)候,當(dāng)你打電話給speak(name);的時(shí)候,內(nèi)部會(huì)說(shuō)話。

name = name.concat("4");

它創(chuàng)建了一個(gè)新對(duì)象,因?yàn)镾tring是不可變的。當(dāng)您更改原始字符串時(shí),它將創(chuàng)建一個(gè)新對(duì)象,我同意您返回它,但您沒(méi)有捕獲它。

所以本質(zhì)上你所做的是:

name(new) = name(original) + '4'; // but you should notice that both the names are different objects.

嘗試

String name ="Sam";

name = speak(name);

當(dāng)然,現(xiàn)在我認(rèn)為沒(méi)有必要解釋為什么它與StringBuilder一起工作,除非你不知道StringBuilder是可變的。

"它創(chuàng)建了一個(gè)新對(duì)象,因?yàn)樽址遣豢勺兊摹?#34;在語(yǔ)言中沒(méi)有"可變"的概念。它返回一個(gè)新的對(duì)象,因?yàn)檫@就是該方法的文檔所要做的。它不會(huì)修改它所調(diào)用的對(duì)象,同樣,因?yàn)檫@就是該方法的文檔所要做的。

@很抱歉,先生,我肯定我沒(méi)有你知道的那么多,但是我想如果我說(shuō)"它返回一個(gè)新對(duì)象,因?yàn)樗姆椒ū挥涗浵聛?lái)了",OP會(huì)不高興的。所以這就是為什么我想給出一些背后的理由。

但是我們只說(shuō)類是"不可變的",因?yàn)樗乃蟹椒ǘ急挥涗洖椴恍薷乃K哉f(shuō)方法不修改它是循環(huán)推理,因?yàn)樗遣豢勺兊摹?/p>

@紐阿克特可能是"雞還是蛋":p

看看JavaDoc for String,你會(huì)發(fā)現(xiàn)

[...] String objects are immutable [...].

這意味著concat(String)并沒(méi)有改變String本身,而是構(gòu)建了一個(gè)新的String。

另一方面,StringBuilders是可變的。通過(guò)調(diào)用append(CharSequence),對(duì)象本身就發(fā)生了變化。

好的,speak方法在做什么?

首先,

name.concat("4");

創(chuàng)建新對(duì)象,該對(duì)象等于name,與"4"連接。

所以,這條線

name = name.concat(4);

重新定義局部(用于speak方法)變量name。

然后返回對(duì)這個(gè)新值的引用

return name;

因此,方法中傳遞的原始變量不會(huì)被修改,但該方法返回修改后的值。

在test方法中,實(shí)際上修改變量而不修改引用(StringBuilder類是可變的,因此如果可以修改此類型,則是可變的)。

然后我們可以看到另一個(gè)問(wèn)題出現(xiàn)了:為什么StringBuilder.append返回值,而這個(gè)值似乎是多余的。這個(gè)問(wèn)題的答案在于對(duì)"構(gòu)建器"模式的描述,這是實(shí)現(xiàn)修改方法的常用方法。參見(jiàn)維基百科的建設(shè)者模式。

在方法speak中,concat方法返回一個(gè)新字符串,它調(diào)用的原始對(duì)象不變(字符串是不可變的)。如文件所示:

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")相當(dāng)于name +"4"。

在您的test方法中,append方法修改StringBuilder的內(nèi)容。如文件所示:

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仍然是方法調(diào)用之前的同一對(duì)象,但name的內(nèi)容不變,因?yàn)樽址遣豢勺兊?#xff0c;而names的內(nèi)容已更改。

如果您使用了這兩個(gè)方法的返回值,那么您將得到預(yù)期的結(jié)果。

由于String是不可變的,因此String#concat不修改原來(lái)的字符串實(shí)例,它只返回一個(gè)新的String,而原來(lái)的則保持不變,而StringBuilder是可變的,其變化反映在作為參數(shù)傳遞的StringBuilder實(shí)例中。

在Java中,EDCOX1的1Ω是不可變的。一旦對(duì)名稱調(diào)用concat方法。將創(chuàng)建一個(gè)新字符串,當(dāng)您在System.out.println(name)中使用舊的引用時(shí)。如果要使用修改后的字符串,應(yīng)顯式返回引用。雖然StringBuilder是可變的,它總是返回相同的引用。

當(dāng)調(diào)用speak(name)時(shí),它計(jì)算新值,但丟棄它。

如果你把它換成

name = speak(name);

結(jié)果就是你所期望的。

使用StringBuilder時(shí),您傳遞的對(duì)象是可變的:所以

names.append(names);

更改當(dāng)前對(duì)象的狀態(tài)(它還返回對(duì)同一對(duì)象的引用,這只是方便您編寫(xiě)代碼,如names.append(...).append(...)等)。因此,在StringBuilder的情況下,調(diào)用方法時(shí)所引用的對(duì)象實(shí)際上已經(jīng)更改,因此您可以看到更改。

您沒(méi)有回答這個(gè)問(wèn)題:為什么它與StringBuilder一起工作?

是的,是真的。參見(jiàn)更新。

首先,EDCOX1×0是Java中不可變的類。不可變類只是不能修改其實(shí)例的類。實(shí)例中的所有信息在創(chuàng)建實(shí)例時(shí)初始化,不能修改。

第二,Java參數(shù)是通過(guò)值發(fā)送的,而不是通過(guò)引用發(fā)送的。

在你的"測(cè)試"方法中,你不需要names = names.append("4"),相反,names.append("4")就足夠了。

如果您檢查Java對(duì)象的字符串對(duì)象,您將看到其中的大多數(shù)方法,包括CONTAT,將生成一個(gè)新的字符串。

因此,要讓輸出sam4也用于字符串,您需要在主方法中使用這個(gè)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

.

有關(guān)詳細(xì)信息,請(qǐng)檢查此

結(jié)論:您不需要再將該值重新分配給StringBuilder,因?yàn)樗呀?jīng)是一個(gè)引用測(cè)試方法應(yīng)為

public static void test(StringBuilder names) {

names.append("4");

}

但說(shuō)話應(yīng)該是

String name ="Sam";

name = ?speak(name);

這不是被問(wèn)到的問(wèn)題。

好的,我更新了答案

總結(jié)

以上是生活随笔為你收集整理的java值传递string_关于java:按值传递(StringBuilder与String)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。