是的,为了简化起见,您可以将其视为从同一地址选择,但更精确的是变量拥有相同的 引用 ,即JVM 在映射到对象的正确内存地址时 使用的 数字/对象ID
(对象可以在内存中移动但仍然会有相同的参考)。
您可以使用以下代码进行测试:
String w1 = "word";String w2 = "word";String b = new String("word"); // explicitly created String (by `new` operator) // won't be placed in string pool automaticallySystem.out.println(w1 == w2); // true -> variables hold same referenceSystem.out.println(w1 == b); // false -> variable hold different references, // so they represent different objectsb = b.intern(); // checks if pool contains this string, if not puts this string in pool, // then returns reference of string from pool and stores it in `b` variableSystem.out.println(w1 == b); // true -> now b holds same reference as w1


