In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use equals, = = and intern () of String class". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use equals, = = and intern () of the String class"!
Generation of Java instance
As we all know, an instance of a class new in java is done in the heap of JVM, as shown in the following figure:
Here we take the string class as an example to explain something in more detail!
The code for String to generate an instance is as follows:
String str=new String ("hello")
For generating a string through new (assuming "hello"), first go to the constant pool in the figure above to see if there is already a "hello" object, if not, create a string object in the constant pool, and then create a copy object of the "hello" object in the constant pool in the heap.
= = and equlas ()
Let's look at the following code
String str1=new String ("hello")
String str2=new String ("hello")
System.out.println (str1==str2)
System.out.println (str1.equals (str2))
Output result:
False
True
Str1 and str2 are references (references can be understood as pointers, pointing to the address in memory where "hello" is located), because new String ("hello") is equivalent to opening up a new memory space in the heap, and then storing "hello" in the newly opened memory space. Str1 and str2 point to different memory addresses, regardless of whether the values they store in memory space are equal! Corresponding to = = is the method equals (), which compares the values of String, because the values of S1 and S2 are hello, so they are equal!
Advanced intern ()
Next, let's take a look at the more advanced method intern (). Take a look at the following example
Strings1= "hello"
Strings3=new String ("hello")
System.out.println (s1==s3)
System.out.println (s1==s3.intern ())
Output result:
False
True
The first result, false, is easy to understand because S1 and S3 have different reference addresses! S3 calls the intern () method, which is equivalent to adding a run-time created string to the string constant pool (if it's not already in the pool). So S1 and S3 both point to the same address in the constant pool, so they are equal!
At this point, I believe you have a deeper understanding of "how to use equals, =, and intern () of the String class, so you might as well do it in practice!" Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.