


The String class in Java is a very commonly used class, but the least attention is paid to its details, so most interviews will focus on this class. For example, String str = new String("hello"); opens up several memory spaces, the difference between String and StringBuffer, etc. Here is my understanding:
String is a class modified by final and cannot be inherited. StringBuffer is also a class modified by final.
1. JVM memory division
There are mainly 4 pieces of memory in java. These memory spaces are: stack memory space, heap memory space, global data area, and global code area
1. Stack memory space: Save all object names (the address of the referenced heap memory space is saved)
2. Heap memory space: Save the specific content of each object
3. Global data area: Save static type data attributes (global data)
4. Global code area: Save all method definitions
In the JVM, heap memory is the memory space that stores the content of object instantiation (program data). Stack memory stores the name of the object, and its content points to the address of the corresponding heap.
It can also be said that: all object names are stored in stack memory, and the specific contents of the objects are maintained in heap memory. Reference type data must use the new keyword to open up space in heap memory.
2. Memory allocation of Sring
String has a special feature: when constructing a String object, you can use new construction or "hello" direct construction. Of the two methods, it is recommended to use the second one.
1. String a = "hello";
2. String a= new String("hello");
The explanation is as follows:
1: An object reference a is defined in the stack memory, pointing to the memory address of the value "hello" in the heap memory. Finally a memory space was opened up
2: An object reference of a is redefined in the stack memory, first pointing to the memory address of the heap memory with the value "hello", and then pointing to the address of the heap memory with the value of "hello" after new. In the end, two spaces were opened up. The first space has no object references and will be garbage collected by the JVM.
The diagram is as follows:
It is not difficult to understand the following codes after understanding the above:
package andy.string.test; /** * @author Zhang,Tianyou * version:2014-11-25 下午4:15:14 * * */ public class TestString { public static void main(String[] args){ String a = "hello"; String b = "hello"; String c = new String("hello"); String d = new String(); d = "hello"; String e = c; System.out.println("a==b ? " + (a== b)); System.out.println("a==c ? " + (c== b)); System.out.println("a==d ? " + (a== d)); System.out.println("a==e ? " + (a== e)); System.out.println("c==d ? " + (c== d)); System.out.println("c==e ? " + (c== e)); } }
##Among them only a==b==d and c=e.
The difference is that StringBuilder is not thread synchronized, so its operation must be more efficient than StringBuffer.
This is what some people will think:
String str = "hello"; Hasn’t it also changed?
Therefore, when cyclically assigning values to a string, it is best to use StringBuffer (thread-safe) or StringBuilder, which can save memory and improve performance. Remember
. 
Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software