java - String s=new String()与String s = ""的区别
阿神
阿神 2017-04-18 10:50:36
0
4
745

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

阿神
阿神

闭关修行中......

reply all(4)
左手右手慢动作

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.

阿神
  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.

洪涛

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

迷茫

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template