What is the difference between "=" and "equals" methods?

Source: Internet
Author: User

"=" What is the difference between equals and equals?

= Determines whether two variables or instances point to the same memory space
Equals is used to determine whether the values of the two variables or the memory space pointed to by the instance are the same

= Compares memory addresses
Squals () is to compare the content of a string

= Whether the guidance is the same
Equals () indicates whether the values are the same

A. = The operator is used to compare whether the values of variables are equal. Because the variable name is only a reference name of the memory address, it does not represent the variable itself,
From this we can know that the value of the variable refers to an internal address. Please refer to the example.

Int INTA = 10;

Int intb = 10;

Int intarra [] = new int [2];

Int intarrb [] = new int [2];

For (INT I = 0; I <2; I ++ ){

Intarra [I] = 0;

Intarrb [I] = 0;

}

System. Out. println ("(INTA = intb) =" + (INTA = intb ));

System. Out. println ("(intarra = intarrb) =" + (intarra = intarrb ));

System. Out. println ("(intarra [0] = intarrb [0]) =" + (intarra [0] = intarrb [0]);

The output result is:

(INTA = intb) = true

(Intarra = intarrb) = false

(Intarra [0] = intarrb [0]) = true

The results show that intarra and intarrb are array names, so they represent memory pointers. Therefore, the above conclusions can be found here.

Because intarra [0] And intarrb [0] are at different addresses, it seems that (intarra [0] = intarrb [0]) is false, however, intarra [0] And intarrb [0] only represent

Therefore, (intarra [0] = intarrb [0]) should be true.

Therefore, the = Operator is used to compare whether the "content" or "substance" of a variable is equal;

For example, the INTA content is 10, and the intb content is 10. Therefore, INTA and intb are equal,

Intarra is an Array Memory area of the int type, and the content of intarrb is also an Array Memory area of the int type, and the length is equal, but they are not the same

In the region, but in two different places of memory, so they are not equal

B. Equals () method

The equals () method is the object method. Because the base class of all classes is the object method, the equals method must exist in any class, and the user inherits a certain class and determines

The function of the equals method is determined by the user,

However, we didn't define it, and all the classes we want to define have not been redefined by your class, so its function is the same as that of the object method, because the object uses = by default

Operator. So if your own class does not overwrite the equals method, you will get the same result when you use equals and =.

So to sum up a sentence, = is "content" (or substantive), while equals is the representation.

==And equals () Usage
Reward score: 10-resolution time:
Class
{
Int A = 1;
}
Class Appa
{
Public static void main (string ARGs [])
{
String S1, S2, S3, S4;
S1 = "ABC ";
S2 = "ABC ";
S3 = new string ("ABC ");
S4 = new string ("ABC ");
A obj1, obj2;
Obj1 = new ();
Obj2 = new ();
System. Out. println ("S1 = S2:" + (S1 = S2 ));
}
}
If = is referenced in the document, their references are different! The output should be false! However, the output is true! Let's take a look at the difference between = and equals!
= Whether the guidance is the same
Equals () indicates whether the values are the same

The string is special ~
The string is an object. When creating a new "ABC" string, the system will first go to the memory to check whether 'A', 'B', and 'C' exist. If yes, reference, no new
So what you typed is true.

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.