On String Object in Java

On String Object in Java

Part of the original code of the String class

First look at the source code of the String class

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence,Constable, ConstantDesc {
    @Stable
    private final byte[] value;
    
    private final byte coder;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    private boolean hashIsZero; // Default to false;
    
    private static final long serialVersionUID = -6849794470754667710L;
    ...

It can be seen from the source code that the value character array of String type is final modified, which means that the String type cannot be changed after it is created.

Look at a simple code

The following is a simple piece of judgment code about String objects

// An highlighted block
public static void main(String[] $s) {
        String A = "StringA";
        String B = A;
        //"StringA"已存在,并不会创建新的对象
        String C = "StringA";
        System.out.println("A = B:" + (A == B));
        System.out.println("A = C:" + (A == C));
        //下面比较jvm会先从堆中寻找是否有"StringA"
        System.out.println("A = 'StringA':" + (A == "StringA"));
        //下面比较jvm会直接创建一个新的String类型的对象,将该对象的引用返回给用户
        System.out.println("A = 'new StringA':" + (A == new String("StringA")));
    }

Program running results

A = B: true _
A = C: true
A = 'StringA': true
A = 'new StringA': false
A = 'StringA.intern ()': true

Analysis
(1) First, after String A = "StringA" is executed, a String object with a value of "StringA" is created, the string constant value is added to the string constant pool in memory, and the String object is referenced with " A "Bind.
(2) After String B = A is executed, assign reference A to B. At this time, A and B are the same reference, and both point to the newly created string "StringA"
(3) String C = "StringA" after execution , Java virtual machine first finds whether there is a string constant with the value "StringA" from the string constant pool. If there is, it will directly point "Reference C" to the constant, at this time A, B and C point to "StringA" "Object.
(4) For '==' if design basic types, compare their values; if it involves comparison of reference types, it will compare whether they point to the same object, so at this time A == B == C;
(5 ) When judging (A == "StringA"), jvmhui will first look for a string constant with a value of "StringA" from the string constant pool, if any, it will directly return the reference of the String object, so A = = "StringA" is true;
(6) When judging (A == new String ("StringA")), jvm will immediately create a new String object and put it in the heap to return the reference, so both The address pointed to is not the same, the result is false;
(7) When judging (A == 'StringA'.intern ()), string.

At last

This article is just a brief look at some of the mechanisms of the String class. Please indicate if there are errors.
The reference link for this article: https://blog.csdn.net/qq_34490018/article/details/82110578 .

Published 19 original articles · Likes6 · Visits 728

Guess you like

Origin blog.csdn.net/qq_42631607/article/details/102924590