Java string equals, intern Method

Source: Internet
Author: User

Differences between equals and = in Java

 

= Compare the addresses of two objects, while equals compares the content of two objects.

Obviously, when equals is true, ==is not necessarily set to true;

 

The importance of basic knowledge is expected to attract everyone's attention, including yourself.

Many puzzles and questions come from the most basic knowledge.

After a while, I checked the books and finally got some insights on the special object string.

Public class teststring {

Public static void main (string [] ARGs ){

String S1 = "Monday ";

String S2 = "Monday ";

}

}

What's the problem?

1. worries from string

In the above section, how many objects are there?

Many people may blurted out: Two, S1 and S2.

Why?

String is a final class, and its value is unchangeable.

It seems quite reasonable. Let's check it and change the program slightly.

You can see the result:

Public class teststring {

Public static void main (string [] ARGs ){

String S1 = "Monday ";

String S2 = "Monday ";

If (S1 = S2)

System. Out. println ("S1 = S2 ");

Else

System. Out. println ("S1! = S2 ");

}

}

A lot of people will say there are more than two objects.

Compile and run the program. Output: S1 = S2

Ah!

Why S1 = S2?

= It clearly means that S1 and S2 reference the same string object -- "Monday "!

2. ever-changing string

If you change the program a little bit, you may find it strange:

Public class teststring {

Public static void main (string [] ARGs ){

String S1 = "Monday ";

String S2 = new string ("Monday ");

If (S1 = S2)

System. Out. println ("S1 = S2 ");

Else

System. Out. println ("S1! = S2 ");

If (s1.equals (S2 ))

System. Out. println ("S1 equals S2 ");

Else

System. Out. println ("S1 not equals S2 ");

}

}

We will create S2 with the new operator

Program output:

S1! = S2

S1 equals S2

Well, obviously.

S1 S2 references two "Monday" string objects respectively.

But why are the two procedures different?

3. Swimming in the string Swimming Pool

Haha, I finally found the answer after I flipped through the book:

It turns out that a string buffer pool will be created when the program is running.

When S2 = "Monday" is used to create a string, the program first

Find the object with the same value in the string buffer pool. In the first program, S1 is first

So when S2 is created, the program finds S1 with the same value.

Reference S2 to the object referenced by S1 "Monday"

In the second program, the new operator is used, and he clearly tells the program:

"I want a new one! Don't be old !" And is a new "Monday" sting object created

Built in memory. They have the same value but different locations. A swimming pool

Rest on the shore. Oh, it's a waste of resources. What should we do separately?

4. Continue diving

Change the program again:

Public class teststring {

Public static void main (string [] ARGs ){

String S1 = "Monday ";

String S2 = new string ("Monday ");

S2 = s2.intern ();

If (S1 = S2)

System. Out. println ("S1 = S2 ");

Else

System. Out. println ("S1! = S2 ");

If (s1.equals (S2 ))

System. Out. println ("S1 equals S2 ");

Else

System. Out. println ("S1 not equals S2 ");

}

}

Join this time: S2 = s2.intern ();

Wow! Program output:

S1 = S2

S1 equals S2

It turns out that after the S2 program is created, intern () is used to flip it into the pool.

Haha, This Time S2 and S1 have referenced the same object.

We have successfully reduced memory usage.

5. = battle with equals ()

String is an object. We need to compare whether the values of two different string objects are the same.

Obviously, the equals () method is used.

However, if there are so many string objects in the program, equals will be used for so many times,

Oh, my God, it's really slow.

Better solution:

Set all strings to intern () to the buffer pool.

It is best to perform this operation when new is used.

String S2 = new string ("Monday"). Intern ();

Well, are you all soaking in the pool? Haha

Now I can use = to compare the value of a string object with no scruples.

It's so refreshing, fast, and convenient!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.