Android~BAT, etc




GitHub Link: Contact me about me



In-depth Understanding of the Java Virtual Machine: ADVANCED JVM Features and Best Practices PDF

Too detailed. The average person should not use very detailed. Suitable for improving)
















The Java memory model can be used to create strings.

  • Create quotes -> String text = “bloom”

  • String text = new String()

  • -> String text = new String(“slaughter”)

  • -> String text = “flowers” + “dawn”

The String constant pool

In the method area of Java’s memory structure, there is an area called the Constant Pool. Used to store various constants in the program, including Class, String, Integer and other Java basic data types.

String Pool is the area of the constant Pool responsible for storing String constants

The mechanism by which quotes create strings

String s1 = "bloom";
String s2 = "bloom";
System.out.println(s1 == s2);   //true
Copy the code
  1. Compile-time: The string “bloom” is a compile-time constant whose value is confirmed at compile time

  2. Runtime: The JVM maintains and queries the constant pool and returns a reference to the memory address directly to the variable if there is a change in the pool. If not, the JVM will create the change in the string constant pool and pass the reference to the declaration. Since “bloom” has been in the string constant pool since the program started, the assignment to S1 passes a memory reference from the constant pool to S1, as well as s2. This series of procedures is implemented using the native method intern() in the String class

Throughout the process, all String values are in the constant pool of the method area, and the declared variable reference address also points to the memory address of the value in the constant pool

Create using new and a combination of new and quotes

In Java, when a new object is created using the new keyword, a space is allocated in the Heap to store the object corresponding to the new. This rule also applies to strings.

So a string object created with the new keyword has a memory address corresponding to some memory space in the heap, and its value is stored in the memory space instead of the constant area. Even if the value exists in the constant area.

So in general, it’s not recommended to use the new keyword to create string objects without a specific purpose. If you need to modify string objects frequently, you can use StringBuffer(thread-safe) or StringBuilder(non-thread-safe but more efficient).





Above PDF document + Github link: About me