1, String a = “123”; String b = “123”; A = = b?

Memory, String storage, etc

Conclusion:

String a = "123";
String b = "123"; System.out.println(a == b); The answer:true
Copy the code

The memory managed by the Java VIRTUAL machine contains the following run-time data areas:

One section in the method area is the run-time constant pool. One piece of information in the Class file is the constant pool table, which holds the various literals and symbolic references generated at compile time and is stored in the runtime constant pool in the method area after the Class is loaded.

  • String a = "123";
    • 123: Determinable content for compile time will be maintained in the constant pool;
    • A: Refers to a reference to the address of the data to the right of the equal sign in the constant pool

String b = “123”; It will first look for 123 in the constant pool, and if it finds it, it will direct the b reference to the changed address.

If there is any data to be created in the constant pool, it will return the address of the data. If there is no data to be created, it will create one

        String a = new String("123");
        String b = new String("123"); System.out.println(a == b); The answer:false
Copy the code

Because newly created objects are stored in the heap, there is no caching mechanism described above.