Java 函数中字符串值的变化
我有一个非常尴尬的问题...
void changeString(String str){
str = "Hello world":
}
main(){
String myStr = new String("");
changeString(myStr);
}
当 main
返回时,值仍然是 ""
而不是 "Hello world"
。这是为什么?
另外,我该如何让它发挥作用?假设我希望我的函数 changeString
将它得到的字符串更改为“Hello world”。
I have this very awkward question...
void changeString(String str){
str = "Hello world":
}
main(){
String myStr = new String("");
changeString(myStr);
}
When main
returns, the value is still ""
and not "Hello world"
. Why is that?
Also, how do I make it work? Let's say I want my function changeString
to change the string it got to "Hello world".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
每个人都解释了为什么它不起作用,但没有人解释如何让它起作用。最简单的选择是使用:
尽管这里的方法名称用词不当。如果您要使用您最初的想法,您需要类似的东西:
您的 ChangeableString 类可能是这样的:
关于引用的快速课程:
在 Java 方法中,一切都是按值传递的。这包括参考文献。这可以通过这两种不同的方法来说明:
如果您从
main()
(或任何与此相关的地方)调用doNothing(obj)
,obj
不会在被调用者中更改,因为doNothing
创建一个新的Thing
并将该新引用分配给obj
方法。另一方面,在
doSomething
中,您正在调用obj.changeMe()
,并且取消引用obj
(按值传递)并进行更改它。Everyone explained why it doesn't work, but nobody explained how to make it work. Your easiest option is to use:
Although the method name is a misnomer here. If you were to use your original idea, you'd need something like:
Your
ChangeableString
class could be something like this:A quick lesson on references:
In Java method everything is passed by value. This includes references. This can be illustrated by these two different methods:
If you call
doNothing(obj)
frommain()
(or anywhere for that matter),obj
won't be changed in the callee becausedoNothing
creates a newThing
and assigns that new reference toobj
in the scope of the method.On the other hand, in
doSomething
you are callingobj.changeMe()
, and that dereferencesobj
- which was passed by value - and changes it.Java 使用按值调用策略来评估调用。
也就是说,该值被复制到
str
,因此,如果您分配给str
,则不会更改原始值。Java uses a call by value startegy for evaluating calls.
That is, the value is copied to
str
, so if you assign tostr
that doesn't change the original value.如果经常发生
String
的更改,您还可以将StringBuffer
或StringBuilder
分配给变量并更改其内容,然后仅将其转换为需要时使用String
。If the changing of your
String
happens very often you could also assign aStringBuffer
orStringBuilder
to your variable and change its contents and only convert it to aString
when this is needed.扩展一下 NullUserException 的优秀答案,这是一个更通用的解决方案:
Yura 的原始代码可以重写为:
并且,只是为了好玩,这里是 Scala :
Expanding a bit on NullUserException's excellent answer, here's a more general solution:
Yura's original code can then be rewritten as:
And, just for fun, here it is in Scala:
因为引用
myStr
是按值传递给函数changeString
的,并且更改不会反映回调用函数。PS:我不是Java人。
Because the reference
myStr
is passed by value to the functionchangeString
and the change is not reflected back to the calling function.P.S : I am not a Java guy.
Bill,我对你的问题有一个解决方案,它在 java 中使用 List 作为指针!
这个答案需要一些额外的工作来插入和从列表中取出字符串,但是最后一行将打印“Hello world!”
我希望这也能帮助其他人!
-端口转发播客
Bill, I have a solution to your problem which uses a List as a pointer in java!
This answer takes a little extra work to insert and get out the string from the list, however the final line will print "Hello world!"
I hope this can help others as well!
-Port Forward Podcast
这是 StringBuffer/StringBuilder 为我工作的又一个解决方案。
Here's the one more solution by
StringBuffer/StringBuilder
worked for me.