Java中具有相同值的字符串? [英] Strings of Same Value in Java?

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

问题描述

一个快速而令人困惑的问题.如果A级和B级内部包含此内容:-

a quick and confusing question. If Class A and Class B have this inside them:-

String name="SomeName"; 

并且都实例化了这两个类,是否确实当我们执行objA.name或objB.name时,两个实例都引用了变量名称"的相同存储位置?哪个具有"SomeName"值,并且由于String是不可变的,所以同一JVM的两个类的多个实例重复使用同一变量?我在网上某处读到,除非有

and both classes are instantiated, is it true that both instances refer to same memory location of variable "name" say when we do this objA.name or objB.name ? which has value "SomeName" and since String is immutable, several instances of both classes of same JVM use the same variable repeatedly? I read somewhere online that, unless there is

String example=new String("something"); 

使用

时,前一个声明始终创建一个副本,直到所有其应用程序终止以回收内存为止,该声明都会一直使用.注意:我看到几个答案,我可以指望有人可以得出结论.谢谢大家的努力,谢谢.

is used, the former declaration always creates one copy and it is used until all its applications are terminated for reclaiming memory. Note: I see several answers, which one do I count on, can someone conclude. Thank you all for your effort, appreciate it.

推荐答案

是的,如果您创建两个字符串,如:

Yes, if you create two strings like:

String a = "Hello";
String b = "Hello";

它们将是完全相同的对象.您可以通过

They will be the exact same object. You can test it yourself by doing

System.out.println(a == b);

System.out.println(a == b);

如果它们是同一对象,则它们对字符数组的内部引用将完全相同.

If they are the same object, then their internal reference to the character array will be exactly the same.

现在,如果您做了 String c ="Hell" +"o"; ,则它将没有相同的引用,因为它是(内部)使用StringBuilder构建的.

Now, if you did String c = "Hell" + "o";, it would not have the same reference since it would have been (internally) built using StringBuilder.

此处

相关部分具有(注意:以下内容是从该网站复制的):

The relevant sections has (Note: The following is copied from that web site):

如上所述,有两种构造字符串的方法:通过分配String文字进行隐式构造,或通过new运算符和构造函数显式创建String对象.例如,

As mentioned, there are two ways to construct a string: implicit construction by assigning a String literal or explicitly creating a String object via the new operator and constructor. For example,

String s1 = "Hello";              // String literal
String s2 = "Hello";              // String literal
String s3 = s1;                   // same reference
String s4 = new String("Hello");  // String object
String s5 = new String("Hello");  // String object

Java设计了一种特殊的机制,用于将String文字保留在所谓的字符串公共池中.如果两个String文字具有相同的内容,则它们将在公共池中共享相同的存储位置.采用这种方法可以节省经常使用的字符串的存储空间.另一方面,通过new运算符创建的String对象保留在堆中.就像其他任何对象一样,堆中的每个String对象都有其自己的存储空间.即使两个String对象具有相同的内容,也不会共享堆中的存储.您可以使用String类的equals()方法比较两个String的内容.您可以使用关系相等运算符'=='比较两个对象的引用(或指针).研究以下代码:

Java has designed a special mechanism for keeping the String literals - in a so-called string common pool. If two String literals have the same contents, they will share the same storage locations inside the common pool. This approach is adopted to conserve storage for frequently-used strings. On the other hands, String object created via the new operator are kept in the heap. Each String object in the heap has its own storage just like any other object. There is no sharing of storage in heap even if two String objects have the same contents. You can use the method equals() of the String class to compare the contents of two Strings. You can use the relational equality operator '==' to compare the references (or pointers) of two objects. Study the following codes:

s1 == s1;         // true, same pointer
s1 == s2;         // true, s1 and s1 share storage in common pool
s1 == s3;         // true, s3 is assigned same pointer as s1
s1.equals(s3);    // true, same contents
s1 == s4;         // false, different pointers
s1.equals(s4);    // true, same contents
s4 == s5;         // false, different pointers in heap
s4.equals(s5);    // true, same contents


编辑以添加:运行此SSCE来测试两个常量字符串在不同类中的引用相等性:


Edit to add: Run this SSCE to test reference equality between two constant strings in to different classes:

class T {
  String string = "Hello";

  public static void main(String args[]) {
    T t = new T();
    T2 t2 = new T2();
    System.out.println(t.string == t2.string);
  }
}


class T2 {
  String string = "Hello";
}

打印出 true .

这篇关于Java中具有相同值的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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