relational operators = = and! = Makes a comparison for all objects, returns a Boolean value that often confuses the first-time programmer who touches Java. Here is an example
public class Equalstest {
public static void Main (string[] args) {
Integer in1 = new Integer (100);
Integer in2 = new Integer (100);
System.out.println (in1==in2);
System.out.println (in1!=in2);
}
}
The statement System.out.println (N1==N2) Prints the result of the Boolean value of the comparison in parentheses. The reader may consider the output to be true first, and then false, since two integer objects are the same. However, although the object's content is the same, the object's reference (each new object is a reference) is different, and = = and! = The reference to the object is compared . All output results are actually first false and then true. In fact, the main thing here is to understand = = This operation of the relationship is enough.
When it comes to the = = relational operator, we'll definitely think of the Equals () method, and look at the following example:
public class Equalstest {
public static void Main (string[] args) {
Integer in1 = new Integer (100);
Integer in2 = new Integer (100);
System.out.println (In1.equals (in2));
}
}
The result of this output is true. The question is, can we get this conclusion here:
The ' = = ' relational operator compares a reference to an object, and Equals () compares the actual contents of the object.
Before jumping to conclusions, let's take a look at the following example: public class Equalstest {
Public static void Main (string[] args) {
Value V1 = new value (+);
Value v2 = new value (+);
System.out.println ("V1.equals (v2)?" "+v1.equals (v2));
}
}
class value{
int i;
Public Value (int i) {
this.i = i;
}
}
unexpectedly, the output here is actually false, which makes it hard to understand! The above conclusion is not correct, ah, so that the Equals () A little pit dad ah. To figure this out, let's track down the Equals () method.
first we have to understand where this equals is coming from, the above code we can see the value of this object does not have the Equals () method, then where does equals go from?
If you do not declare a class to inherit when you create the class, then Java inherits it from the object class by default. So object is the parent class for all classes.
So we understand that equals () is from the parent class of the value class object, so let's look at the Equals () method of object:
As you can see from the code above, the Equals method of object compares the objects ' references to the actual contents of the object, and one of our questions is:
Why does the Equals () method of the integer compare the actual contents of an object?
To answer this question, let's look at the original code of the Integer class:
By the four above we know that the integer has overridden the Equals () method of object, so that the Equals () method of Interger compares the actual content value of the object (int value).
Now I will also rewrite the value of the Equals () method, the code is as follows:publicclass Equalstest {
Public static void Main (string[] args) {
Value V1 = new value (+);
Value v2 = new value (+);
System.out.println (V1.equals (v2));
}
}
class value{
int i;
public int GetValue () {
return i;
}
Public Value () {
}
Public Value (int i) {
this.i = i;
}
Public Boolean equals (Object obj) {
if (obj instanceof Value) {
return i = = ((Value) obj). GetValue ();
}
return false;
}
}
The result of the output is the result we want: true.
At this point we can draw the conclusion that:
The ' = = ' relational operator compares a reference to an object, while the Equals () of the object class is also a reference to the objects, and if you want to compare the actual contents of the object, you can override the parent class
The Equals () method of the (Object), Eg:java the JDK's own class: Integer,string, and so on.
Note:
When the value of integer i is within the range of [-128,128], Java will place the variable i as a variable and put it into memory, but if it is not within this range, it will go to the new integer object,
So when the Ingeter value is within the range of [-128,128], ' = = ' compares the actual content value of the object, which is the value of the variable instead of the comparison reference.
This is my first blog, write bad, may also have written in the wrong place, please readers can give valuable advice, I write blog is to deepen the impression and understanding of some things.
2017/8/23
Java equals () and ' = = ', '! = ' for use and error-prone