The difference between equals and = = in Java
= = compares the addresses of 2 objects, while equals compares the contents of 2 objects.
Obviously, when equals is true, = = is not necessarily true;
The importance of basic knowledge, hope to attract people's attention, including their own
A lot of confusion and doubt and all comes from the most basic knowledge
Toss for a while and check the book, finally to the String this special object has a bit of sentiment
public class TestString {
public static void Main (string[] args) {
String S1 = "Monday";
String s2 = "Monday";
}
}
What's the problem?
1. Concerns from String
How many objects are there in this procedure?
Probably a lot of people blurt out: two, S1 and S2
Why?
The String is the final class, and its value is immutable.
It seems to make sense, so check it out, change the program a little bit.
You will 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");
}
}
Oh, a lot of people will say that there are more than two objects
Compile and run the program, output: S1 = = S2
Ah!
why S1 = = s2?
= = Clearly is said: S1 and S2 reference the same String object-"Monday"!
2. The ever-changing String
A little more change to the program, there will be more strange discovery:
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 the S2 with the new operator
Program output:
S1! = S2
S1 equals s2
Well, it's obvious.
S1 s2 references Two "Monday" string objects respectively
But why are the two programs different?
3. Swimming in the pool of String
Haha, turned over the book finally found the answer:
It turns out that a string buffer pool is created when the program runs
When using S2 = "Monday", the expression is to create a string, the program will first
In this string buffer pool to find objects of the same value, in the first program, S1 was first
is placed in the pool, so when S2 is created, the program finds the S1 with the same value
S2 refers to the object referenced by S1 "Monday"
In the second procedure, the new operator is used, and he knows the telling program:
"I want a new one!" Don't be old! "With is a new" Monday "Sting object was created
Built in memory. Their values are the same, but the locations are different and one swims in the pool
A rest on the shore. Alas, it is a waste of resources, obviously the same must be divided into what?
4. Continue the Dive
To 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");
}
}
This time join: S2 = S2.intern ();
Wow! Program output:
S1 = = S2
S1 equals s2
Originally, the program after new S2, and with intern () knocked him in the pool
Haha, this time S2 and S1 have quoted the same object.
We succeeded in reducing the memory footprint.
5. = = battle with Equals ()
String is an object that compares the values of two different string objects
The obvious use of the Equals () method
But if there are so many string objects in the program, there are so many times to use equals,
Oh, my God, it's slow.
A better approach:
Put all the strings intern () to the buffer pool.
It's best to do this when you use new.
String s2 = new String ("Monday"). Intern ();
Well, did everyone soak in the pool? Ha ha
Now I can use = = to compare the value of a String object.
It's so cool, it's quick and convenient!
Equals,intern method for Java string (reproduced)