Difference between equals and = in Java

Source: Internet
Author: User

My understanding:

Equals compares the values of two different objects to be equal.

= Is to compare whether the values of the same two objects are equal.

This is a reference article:

========================================================== ========================================================== ======

After learning Java for a while, I felt like I had started to get into the door. I felt a little confused and confused, and I found that all of my questions came from the basic knowledge. After a while, I checked my books, finally, I have some insights on the special object 'string'. Let's take a look at a strange program:
Public class teststring {
Public static void main (string [] ARGs ){
String S1 = "Monday ";
String S2 = "Monday ";
}
}
This program is really simple! But what's the problem?
1. worries from the string. How many objects are there in the above program?
Maybe many people blurted out: Two, S1 and S2. Why? String is a final class, and its value is unchangeable. It seems quite reasonable, so let's check it. You can see the result by slightly modifying the program:
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 that more than two objects have already been compiled and run the program. Output: S1 = S2!
Why S1 = S2?
= It clearly means that S1 and S2 reference the same string object -- "Monday "!
2. If the ever-changing string changes 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 a program output using the new operator S2:
S1! = S2s1 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 turning through the book:
Original
When the program is running, a string buffer pool is created when S2 = "Monday" is used"
When creating a string, the program first searches for objects with the same value in the string buffer pool. In the first program, S1 is first placed in the pool, so when S2 is created,
The program finds S1 with the same value and references S2 to the second program of the object "Monday" referenced by S1.
Operator, he clearly told the program: "I want a new one! Don't be old !" And is a new "Monday" sting object created in the memory. They have the same value but different positions.
Swim in the pool and rest on the shore. Oh, it's a waste of resources. What should we do separately?
4. Continue diving and 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 = s2s1 equals S2. Originally, after the program was created with S2, it was overturned in the pool with intern (). Haha, This Time S2 and S1
With the same object referenced, we have successfully reduced memory usage. 5. = The Battle string with equals ()
Is an object. To compare whether the values of two different string objects are the same, use equals ()
However, if there are so many string objects in the program, equals is used for many times.
Oh, my God, it's really slow. Better Way: put all the strings into the buffer pool. It's best to perform this operation string S2 = when using new.
New String ("Monday"). Intern (); well, are you all soaking in the pool? Haha now I can use = to compare strings with no scrubs
The object value is really refreshing, fast and convenient!

String, String. What do you want to say? Isn't it enough for you to bring all the troubles to our Java programmers?
Let's see why this time the string is bothering us.
1. Review the bad-tempered string old brother
Routine 1: Class STR {
Public static void main (string [] ARGs ){
String S = "Hi! ";
String T = "Hi! ";
If (S = T)
System. Out. println ("equals ");
Else
System. Out. println ("not equals ");
}
}
What does the program output?
If you have read one of my puzzles from string, I believe you will soon make the right judgment: program output: equals
2. Oh, my God, it's mixing up again.
Routine 2: Class STR {
Public static void main (string [] ARGs ){
String S = "hello ";
String T = S. touppercase ();
If (S = T)
System. Out. println ("equals ");
Else
System. Out. println ("not equals ");
}
}
So what does this program output?
Be careful! Be careful! Don't be confused by the character string!
It outputs: swhy !!!
Simply change the program:
Class str2 {
Public static void main (string [] ARGs ){
String S = "hello ";
String T = S. touppercase ();
If (S = T)
System. Out. println ("equals ");
Else
System. Out. println ("not equals ");
}
}
You may say: Isn't it the same? No! The difference is true! This output:
Not found Soh mygod !!!
Who will teach me this string!
3. Do you know your horse?
"To tame the wild horse, you need to understand its nature," said the cowboy.
You
Do you know about string? Interpreting the string API, we can see: touppercase () and tolowercase ()
Method to return a New String object, which indicates whether the original string is in upper or lower case. However, note that if the original string is in upper or lower case, the original object is returned.
This is why S and T are ambiguous in the second program.
There seems to be no better way for us to dissect it and see what structure it has:
(1) charat (int n) returns the characters at the N position in the string. The first character is 0, and the last character is length ()-1, A huge brick will be thrown at the wrong location: stringindexoutofboundsexception is really big enough.
(2) Concat (string Str) connects a STR after the original object, but returns a New String object.
(3) When signorecase (string Str) ignores case-insensitive equals methods, the essence of this method is to call the static character method touppercase () or tolowercase () to convert the two characters in comparison, then perform the = operation
(4) TRIM () returns a new object, which removes the same blank characters at the beginning and end of the original object. If the result is no different from the original object, the original object is returned.
(5)
Does the tostring () string class also have the tostring () method? This is really an interesting question, but without it, your string
The object may not be used in system. Out. println (). Be careful. It returns its own string.
Class has many other methods, it will bring a lot of convenience, there will be a lot of confusion, so stick to the principle, is the most critical
4. I want to buy a better horse and a younger brother stringbuffer with a more gentle string.
At this time, some people will oppose:
It is very easy to use and highly efficient. How can it be a younger brother?
Very
Simple, it has fewer interaction functions than string. If you want to edit the string, it is not convenient, you will be disappointed with it, but this does not mean it is not powerful public final class.
String implements serializable, comparable, charsequencepublic final
Class stringbuffer implements serializable,
Charsequence is very obvious, younger brother is missing some stuff, but this will not interfere with its future, stringbuffer is not by string
But you should pay attention to it, brother. It's also final. Is it the same root's method? So many stable and reliable methods are much more efficient to use than naughty strings?
Java provides an independent stringbuffer for the string objects to be changed.
The reason for separating the final instances is that the string modification requires the system to increase sales volume and occupy more space, I believe that when 10000 people are crowded in one
Swimming in a small swimming pool, and another 10000 people on the shore are waiting to enter the swimming pool and anxious to get on fire. Another 10000 people are watching the fun, you string
The swimming pool administrator will also be overwhelmed by a simple string without changing the string.
Class is enough for you to call, and when you want to frequently change the content of the string, you need to use the stringbuffer that can support the ship.
5. Support Delivery
(1) The length () in the length () and capacity () string returns the length of the string brother stringbuffer. Are they determined by the length of the characters contained in the object capacity?
Public class testcapacity {
Public static void main (string [] ARGs ){
Stringbuffer Buf = new stringbuffer ("it was the age of wisdom ,");
System. Out. println ("Buf =" + BUF );
System. Out. println ("Buf. Length () =" + Buf. Length ());
System. Out. println ("Buf. Capacity () =" + Buf. Capacity ());
String STR = Buf. tostring ();
System. Out. println ("str =" + Str );
System. Out. println ("str. Length () =" + Str. Length ());
Buf. append ("" + Str. substring (0, 18). append ("foolishness ,");
System. Out. println ("Buf =" + BUF );
System. Out. println ("Buf. Length () =" + Buf. Length ());
System. Out. println ("Buf. Capacity () =" + Buf. Capacity ());
System. Out. println ("str =" + Str );
}
}
Program output:
Buf = It was the age of wisdom. Buf. Length () = 25
Buf. Capacity () = 41
STR = It was the age of wisdomstr. Length () = 25
Buf = It was the age of wisdom, it was the age of foolishness,
Buf. Length () = 56
Buf. Capacity () = 84
STR = It was the age of wisdom,
Yes
As you can see, after the content is changed, capacity also changes the length. With the addition of characters to the string, the capacity increases only after the new length exceeds the current capacity.
The capacity of stringbuffer is automatically changed when the operating system needs it. what programmers can do for capacity is only during initialization.
Stringbuffer object.

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.