Home  >  Q&A  >  body text

java - String s=new String()与String s = ""的区别

是不是定义字符串如果不使用new来初始化的话相同的字符串会被定义成一个引用

阿神阿神2743 days ago696

reply all(4)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:52:36

    JVM for String的存储有一点特殊的地方在于有一块String常量池.
    This constant pool stores references to String objects.

    For example, String s = "abc"会先去String常量池中查找有没有已经存在的引用,如果没有,声明的abc会直接生成一个String对象,并且会在String常量池 stores a reference pointing to this String object.

    Strings declared directly after

    will also follow the above steps, so the second time String s2 = "abc"String常量池 finds a reference pointing to the string object declared for the first time.

    Andnew String("abc")这样会直接在堆中创建新的对象,不会进入String常量池。要把这样的对象引用放入常量池中就涉及另一个String类的方法intern(), this method returns a constant pool reference of a String object. If this object is not in the constant pool, the String object will be put into the constant pool and the corresponding object reference will be returned.

    The first screenshot method of the questionerstr2.intern() == str3.intern()这样使用也会返回true,调用intern()返回的String常量池The reference is the same.

    reply
    0
  • 阿神

    阿神2017-04-18 10:52:36

    1. All non-built-in data types in Java are references.

    2. String s = new String("xx"); This method will create a memory space and make the reference s point to it.

    3. String s = "xx"; This method will make the reference s point to a shared space.

    4. When created using new, str2 and str3 point to different memory spaces, so str2 and str3 are not equal.

    5. When string assignment is used directly, str2 and str3 point to the same memory space, so str2 and str3 are equal.

    6. You can use str2.equals(str3) to compare the contents of strings.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:52:36

    One is to store in the constant pool, and the other is to create new objects in the heap

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:52:36

    Helps you understand the process. There are a few points in Java that you can remember
    1. All strings will be generated in the constant pool, corresponding to CONSTANT_String_info, which is immutable.
    2. Ordinary objects are almost always generated in the heap (of course, there are also some special objects such as Class objects that may be generated in the method area. This depends on different virtual machine implementations, and the virtual machine specification is not mandatory)
    3.= =When making this determination, for reference types, the final analysis is to compare the memory address.

    Okay, now that we have the above concepts. In the first question,
    String s1 = new String("aaa"); When you create a new object, jvm will help you open up an object space on the heap, and s1 is stored in your local variable table, and s1 points to This object space (can be temporarily understood as the address of the object space stored in s1). So you created two new ones, which are two different object spaces. ==The judgment is of course different, because s1 and s2 point to different spaces.

    The second question, the first point above, each string will only exist in one copy in the constant pool, so str2 points to the address of this constant pool string, and str3 also points to the address of this constant pool string. ==The judgment is naturally the same.

    reply
    0
  • Cancelreply