字符串不变性 [英] Strings Immutability

查看:67
本文介绍了字符串不变性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我不能更改Java中的字符串.下面的代码呢?

I was told that strings in java can not be changed.What about the following code?

name="name";
name=name.replace('a', 'i');

它不更改name字符串吗? 另外,replace()的实现在哪里;相比于();等于();假如? 我只是在这里使用这些功能,但是它们实际上在哪里实现?

Does not it changes name string? Also, where is the implementation of the replace(); compareTo(); equals(); provided? I am just using these functions here, but where actually are they implemented?

推荐答案

String.replace()返回新的字符串.

String.replace() returns a new String.

名称"是对String对象的引用,因此可以重新分配它以指向name.replace(),但它将指向一个新对象.

"name" is a reference to a String object, so it can be reassigned to point to name.replace(), but it will be pointing to a new object.

这是 String的javadoc ,您可以在其中找到所有方法的作用.

Here is the javadoc for String, where you can find out what all the methods do.

这篇关于字符串不变性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆