Comparison of equal 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 with the "= =" and "Equals ()" Methods is different.

"= =" 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 function of the "= =" and "equals" methods are the same and are used 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.

The difference between equals and = = is from the following aspects:

(1) If it is the basic type comparison, then 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 the basic type of packaging type, such as Boolean, Character, Byte, Shot, Integer, Long, Float, double and so on the reference variable, = = is the comparison of the address, and equals is the comparative content. Like what:

public class Testequals {

public static void Main (string[] args)

{Integer n1 = new Integer (30);

Integer n2 = new Integer (30);

Integer n3 = new Integer (31);

SYSTEM.OUT.PRINTLN (n1 = = N2);//The result is a false two different integer objects, so their address is different,

SYSTEM.OUT.PRINTLN (n1 = = n3);//Then either the new Integer (30) or the new Integer (31) results show false

System.out.println (N1.equals (n2));//result is true according to the instructions in the JDK document, the contents of N1 and the objects pointed to by N2 are equal, both are 30, so the result of equals comparison is true

System.out.println (N1.equals (n3));//result is false because the object content is different, one is 301 is 31

}

}

This is an example of an 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), the 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 = "123";

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

}

}

Explanation: S1 and S2 respectively 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, and (1) Run result is true, And because S1.equals (S2) is comparing the contents of S1 and S2 objects to be equal, and we know that the contents of both objects are string constant "123", the result of the operation at Mark (2) is true.

The same method of analysis, S1 and S3 point to the object is different, the content is not the same, so the Mark (3) and (4) run 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 has 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.

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.

Comparison of equal and = = in Java

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.