Home Java javaTutorial Comparative analysis of the differences between Java String and new String()

Comparative analysis of the differences between Java String and new String()

Apr 27, 2017 am 09:54 AM
java string

This article mainly introduces relevant information about the difference between Java String and new String(). Friends who need it can refer to it

The difference between Java String and new String()

The stack area stores references and basic types, but cannot store objects, while the heap area stores objects. == compares addresses, and equals() compares object contents.

String str1 = "abcd" implementation process: first create a str reference in the stack area, and then find its pointer in the String pool (which exists independently of the stack and heap and stores invariables) The object whose content is "abcd", if there is no one in the String pool, create one, and then str points to the object in the String pool. If there is, then directly point str1 to "abcd""; if the string variable str2 is later defined = "abcd", then the str2 reference will directly point to the "abcd" that already exists in the String pool, and the object will not be re-created; when str1 is assigned (str1 = "abc"), str1 will no longer point to "abcd". Instead, it re-points to "abc" in the String pool. At this time, if you define String str3 = "abc" and perform str1 == str3 operation, the return value is true, because their values ​​are the same and the addresses are the same, but if the content is "abc" "str1 performs string + connection str1 = str1+"d"; at this time str1 points to the newly created object in the heap with the content "abcd", that is, str1==str2 is performed at this time, and the return value is false, because The address is different.

String str3 = new String("abcd") implementation process: Create the object directly in the heap. If there is String str4 = new String("abcd") later, str4 will not point to it. Instead of the previous object, create an object again and point to it, so if str3==str4 is performed at this time, the return value is false, because the addresses of the two objects are different. If it is str3.equals(str4), it returns true, because The content is the same.

No need to say more, just go to the code:

String str1 = "abcd";
String str2 = "abcd";
String str3 = new String("abcd");
String str4 = new String("abcd");
System.out.println(str1==str2);//true地址一样
System.out.println(str3==str4);//false,但地址不一样
System.out.println(str3.equals(str3));//true,值一样
System.out.println(str2.equals(str3));//true,值一样
System.out.println((str1+"a")==(str2+"a"));//false;进行了+连接地址不一样
Copy after login

The above is the detailed content of Comparative analysis of the differences between Java String and new String(). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

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...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

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...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

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...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles