Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use equals, = = and intern () of the String class

Shulou Source: shulou.com Published: 2022-06-03 00:00:22 09月29日 Update

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!

Tags: Memory constants addresses characters strings objects methods instances pointing space results different advanced code content learning generation output practical deeper Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Shulou Information MySQL Microsoft