如何将java中的字符串证明为引用类型?看看下面的例子
案例1: 这种情况的结果是显而易见的 案例2:如何将java中的字符串证明为引用类型?看看下面的例子,java,string,value-type,reference-type,Java,String,Value Type,Reference Type,案例1: 这种情况的结果是显而易见的 案例2: String s1 = "Hello"; String s2 = s1; //now has the same reference as s1 right? System.out.println(s1); //prints Hello System.out.println(s2); //prints Hello s1 = "hello changed"; //now
String s1 = "Hello";
String s2 = s1; //now has the same reference as s1 right?
System.out.println(s1); //prints Hello
System.out.println(s2); //prints Hello
s1 = "hello changed"; //now changes s2 (so s1 as well because of the same reference?) to Hello changed
System.out.println(s1); //prints Hello changed
System.out.println(s2); //prints Hello (why isn't it changed to Hello changed?)
我想澄清引用类型的混淆。之后
String s1 = "Hello";
String s2 = s1; //now has the same reference as s1 right?
System.out.println(s1); //prints Hello
System.out.println(s2); //prints Hello
s2 = "hello changed"; //now changes s2 (so s1 as well because of the same reference?) to Hello changed
System.out.println(s1); //prints Hello (why isn't it changed to Hello changed?)
System.out.println(s2); //prints Hello changed
s2
和s1
都包含对同一字符串的引用
但之后
String s2 = s1;
s2
保留对新字符串的引用,而s1
仍保留对原始字符串的引用
String
s是不可变的,因此不能更改现有String
对象的状态。将新值赋给字符串
变量只会使该变量引用一个新的字符串
对象。原始的字符串
对象不受影响。好的,让我为您清除以上所有情况
案例1:
当您创建字符串s1=“Hello”编译器首先将字符串值“Hello”
放在内存位置,并将该内存位置的引用存储到变量s1
。那么你的变量s1有什么?仅参考“Hello”
。因此,s1
不包含Hello
,而是作为参考的内存位置。完成了吗
然后当您声明字符串s2=s1
,您只存储Hello
到s2
的引用,而不存储s1
变量本身的引用,因为s1
具有“Hello”
的引用。然后,当您同时打印s1
和s2
时,它们都打印“Hello”
,因为它们都包含“Hello”
的引用
当您声明s1=“hello changed”时
,s1
现在不再引用“Hello”
,而是包含字符串值“Hello changed”
的引用,它位于不同的内存位置。但是s2
仍然引用了String
value“Hello”
,因为您尚未向s2
分配任何内容。因此,现在s1
的引用是“hello changed”
,s2
的引用是“hello”
。如果打印s1
和s2
,它们将打印相应的字符串值“hello changed”
和“hello”`。你现在清楚了吗?如果尚未看到以下代码示例:
s2 = "hello changed";
案例2:
如果你仍然感到困惑,别忘了评论它。谢谢。明白了。如何说java中的字符串是引用类型?你能给我举个例子吗?@TarunDadlani你说的“怎么说”是什么意思<代码>字符串
s是引用类型,因为字符串
是一个类。Java中的所有类和接口都是引用类型。我知道它使用的是类,而不是字符串。你已经看到了这两个cases@TarunDadlani字符串是一个类,所以我不知道你的意思。你的意思是,由于字符串的不变性,你不能证明改变一个对象的状态会影响到所有引用它的变量吗?
String s1 = "Hello"; // s1 has reference of "Hello"
String s2 = s1; // Now s2 has reference of "Hello" not s1
System.out.println(s1); // Prints "Hello"
System.out.println(s2); // Prints "Hello"
s1 = "hello changed"; // Now s1 has reference of "hello changed" not "Hello" but still s2 has reference of "Hello" because you did not changed it.
System.out.println(s1); // Prints "hello changed"
System.out.println(s2); // Prints "Hello" because you did not changed it.
String s1 = "Hello"; // s1 has reference of "Hello"
String s2 = s1; // Now s2 has reference of "Hello" not s1
System.out.println(s1); // Prints "Hello"
System.out.println(s2); // Prints "Hello"
s2 = "hello changed"; // Now s1 has reference of "hello changed" not "Hello" but still s2 has reference of "Hello" because you did not changed it.
System.out.println(s1); // Prints "Hello" because you did not changed it.
System.out.println(s2); // Prints "hello changed" because you changed it.