【发布时间】:2014-10-24 12:13:40
【问题描述】:
为什么 String 类在 Java 中是不可变的,因为每次我们在一个对象上调用某些方法 字符串引用变量,新建字符串?
public class Test {
public static void main(String [] args) {
String s = "abc";
s.toUpperCase(); // new String is created here.
System.out.println(s);// prints abc instead of ABC
}
}
【问题讨论】:
-
你问了一个答案:)
-
阅读字符串的文档。
toUpperCase返回一个新的字符串——原来的没有改变。
标签: java