当前位置:网站首页>The difference between string constants and string objects when allocating memory

The difference between string constants and string objects when allocating memory

2022-07-07 06:24:00 Pei Nanwei_

Catalog

elicit questions

Understand memory distribution

Explain and analyze :

Memory distribution analysis

String s1 = new String("abc")

String s2 = "abc"

String s3 = s2.intern()

Result analysis


elicit questions

Before understanding the difference between the two , Let's take a look at a piece of code

    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = "abc";
        System.out.println(s1 == s2);

        String s3 = s2.intern();
        System.out.println(s2 == s3);
    }

If you were to 2 If you have doubts about the output , After reading this article , I believe you will get something .

Understand memory distribution

Let's first understand the creation process of string constants, string variables and their objects , I believe it will be convenient for you to understand the above comparison .

String constant : It is placed in the string constant pool , In the code, that is "abc"

String object : It's an object , It's in the pile , In the code, that is "new String("abc")"

Next is their distribution in memory

Explain and analyze :

Memory distribution analysis

String s1 = new String("abc")

First, open an address in the stack s1 In the pile new String("abc") The address of , then new This String The object of . stay new When this object , It is necessary to judge whether there is... In the string constant pool first "abc" This constant . If there is , Store the address of this constant directly in the object . without , You need to create this constant in the constant pool first , Then store the memory of this constant in the object .

String s2 = "abc"

First, open an address in the stack s2, The address of the constant to be stored . It is still to go to the constant pool to determine whether this constant exists , The difference is , This time, it is directly stored in the stack s1 quote .

String s3 = s2.intern()

Reference creation is as above ,String Object's intern Method , If the pool already contains one equal to this String Object's string ( The object is created by  equals(Object)  Method determination ), Then return the string in the pool . otherwise , Put this String Object added to pool , And return this String References to objects .

Result analysis

Compare s1 and s2 when , because s1 The object of is new Coming out , So it exists in the heap . and s2 The stored address is in the method area in the character constant pool , So it's more s1 and s2 Address comparison of , It must be false;

Compare s2  and s3 when , because s3 By s2.intern() Come to ,intern We already know that it is a reference to a constant returned directly . therefore s2 and s3 The storage address of is the same , Of course return true

Okay , That's all for this article , Favorite students can like the collection , Have a problem , Can comment , Or leave a message , I will give you feedback at the first time , Thank you for watching. !!

notes : This article is for me to share my learning experience , There are mistakes or areas that need to be corrected , Please correct me. , I will accept with an open mind

原网站

版权声明
本文为[Pei Nanwei_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070141178367.html