Comparative analysis of the differences between Java String and new 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;进行了+连接地址不一样
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!

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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.