Comparison of the Equals method and "= =" in Java

Source: Internet
Author: User

The Equals method is a method of the Java.lang.Object class.

There are two usage notes:

( 1 for string variables, comparing strings by using the "= =" and "Equals ()" methods differs in comparison methods.

"= =" compares the value of two variables themselves, that is, the first address of two objects in memory.

Equals () compares what is contained in the string.

Like what:

String S1,S2,S3 = "abc", S4 = "abc";

S1 = new String ("abc");

S2 = new String ("abc");

So:

S1==S2 is false//two variables have different memory addresses, meaning they point to different objects,

therefore not equal.

S1.equals (S2) is true//two variables that contain the contents of ABC, so it is equal.

Note (1):

if: stringbuffer S1 = new StringBuffer ("a");
StringBuffer s2 = new StringBuffer ("a");

Result: s1.equals (S2)//Is False

Explanation: The Equals method is not redefined in the StringBuffer class, so this method comes from the object class,

The Equals method in the object class is used to compare "address", so it is equal to false.

Note (2):

For S3 and S4, it's a bit different to be noticed, because S3 and S4 are two characters

The variables generated by the string constants, where the memory addresses are equal,

So S3==s4 is true (even if there is no s3=s4 such an assignment statement)


( 2 for non-string variables, the "= =" and "equals" methods have the same effect to compare their

Object at the first address of the heap memory, which is used to compare whether two reference variables point to the same object.

Like what:

Class A

{

A obj1 = new A ();

A obj2 = new A ();

}

Then: Obj1==obj2 is False

Obj1.equals (OBJ2) is False

But add such a sentence: Obj1=obj2;

So Obj1==obj2 is True

Obj1.equals (OBJ2) is true

In summary: The Equals method is the comparison for a string, and for a non-string comparison

Whether the object it points to is the same.

The = = comparison is also the same as the object that the object is pointing to is the first address in memory.

The Equals method is redefined in the string class, and the value is compared instead of the address. So it is true.

about the equals with the == the difference is from the following aspects:

( 1 If it is a basic type comparison, then you can only use = = to compare, cannot use equals

Like what:
public class Testequals {
public static void Main (string[] args)
{
int a = 3;
int b = 4;
int c = 3;
System.out.println (A = = B);//result is False
System.out.println (A = = c);//result is true
System.out.println (A.equals (c));//error, compilation cannot be passed, Equals method
Cannot be used in comparison with basic types
}
}

( 2 )   for basic type of wrapper type, such as Boolean, Character, Byte, Shot, Reference variables, such as Integer, Long, Float, double, and so on, = = are compared to the address, and equals is the comparison content. For example:
public class Testequals {
public static void Main (string[] args)  
{integer N1 = new Integer;
Integer n2 = new Integer (30);
Integer n3 = new Integer (31);
System.out.println (n1 = = N2);//The result is false  two different integer objects, so their address is different,
System.out.println (n1 = n3);//So whether it's new The integer (30) or new integer   result displays false
System.out.println (N1.equals (n2));//The result is true  based on the instructions in the JDK documentation, N1 is equal to the contents of the object pointed to by N2, and is 30, so equals compares the result to True
System.out.println (N1.equals (N3)), and the result is false  because the object content is different, One is 301.
}
}
This is an instance of integer, if it is other such as Double, Character, float, and so on.

( 3 )   Note: for string (String), StringBuffer ( A thread-safe variable character sequence), StringBuilder (variable character sequence), three classes for further explanation.
(a) first, describe the use of String, see the following example:
public class Testequals {
public static void Main (string[] args) {
String S1 = "1 23 ";
String s2 = "123";
String s3 = "ABC";
String s4 = new String ("123");
String s5 = new String ("123");
String s6 = new String ("abc");

System.out.println (S1 = = s2);//(1) True
System.out.println (s1.equals (S2));//(2) True
System.out.println (S1 = = s3);//(3) flase
System.out.println (S1.equals (S3));//(4) flase

System.out.println (S4 = = S5);//(5) Flase
System.out.println (S4.equals (S5));//(6) True
System.out.println (s4 = = s6);//(7) flase
System.out.println (S4.equals (S6));//(8) flase

System.out.println (S1 = = S4);//(9) False
System.out.println (S1.equals (S4));//() True
}
}

Answer explanation: S1 and S2 point to the object created by the string constant "123"  , in the constant pool, there is only one object, the content is 123, there are two references S1 and S2 point to this object, so the two reference variables point to the same address, Thus (1) The result of the operation is true, and because S1.equals (S2) is to compare S1 and S2 The content of the object pointed to is equal, and we know that the contents of both objects are string constant "123", so the mark (2) at the end of the operation is true.
in the same way, S1 and S3 point to different objects, the content is not the same, so the Mark (3) and (4) Run the result is false.
Look at S4 and S5, the objects that the two reference variables point to are the same (the contents are all 123), but the two objects are created in the class with the new operator, which allocates two spaces to the two objects in memory, so the memory addresses of the two objects are different, and the story two distinct objects, The S4 = = s5  at the mark (5) runs the result of false, but the content is the same, so the S4.equals (S5) at the mark (6) results in true. Similarly, S4 and S6 point to different object addresses, and the contents are not the same. The result of marking (7) (8) is false. The
S1 and S4 point to two different objects (the reason for this is that the two objects are not the same in memory, so the objects are not the same), so the S1 = = S4 marked as (9) is run as false and S1.equals (S4) labeled (10) Run result question: At first glance the result, a little surprised, why not true, not to say that the Equals method is the comparison of content?
Explanation: Yes, if the Equals method is overridden in the new class, it can be used to compare the contents. However, in the above example, the class value does not overwrite the Equals method in object, but instead inherits the method, so it is used to compare the address, and V1 and V2 point to the object is not the same, so the mark (1) at the V1.equals (v2) Run the result is false, The V1 = = V2 marked as (2) also runs the result of false.

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.