Usage of equals and = = in Java (brief introduction)

Source: Internet
Author: User

A brief introduction to the Equals method is a method of the Java.lang.Object class with two usages: first, when you compare strings using the "= =" and "Equals ()" Methods for string variables, their comparison methods are different. 1, "= =" compares the value of two variables themselves, that is, the first address of two objects in memory. (in Java, the first address of an object is the start address that it holds in memory, and the address behind it is used to hold the addresses of the various properties it contains, so memory blocks are used to hold each parameter of the object, and the object can be found by this first address, so that the object's individual properties can be found) 2, " Equals () "Compares what is contained in the string.

Like what:

String S1,S2,S3 = "abc", s4= "abc"new string ("abc"New string ("abc"); S1= = S2   false      // Two variables have different memory addresses, meaning they point to different objects,true    // The contents of the two variables are ABC, so they are equal. 

String S1,S2,S3 = "abc", S4 = "abc" ;         New String ("abc");         New String ("abc");        System.out.println ("S1==S2:" + (s1==s2));        System.out.println ("S1==S3:" + (s1==S3));        System.out.println ("S1==S4:" + (s3==S4));        System.out.println ("s1.equals (S2):" +(s1.equals (S2)));        System.out.println ("S1.equals (S3):" +(s1.equals (S3)));        System.out.println ("S3.equals (S4):" + (S3.equals (S4)));

Test diagram

Note:
New StringBuffer ("A"new StringBuffer ("a"); System.out.println ("s1.equals (S2):" + (S1.equals (s2)));//result 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 "addresses", so equals false)Note:For S3 and S4, there is a difference to be noticed, because S3 and S4 are two characters, the variables generated by the string constants, where the memory address is equal, so s3==s4 is true (even if there is no s3=s4 such an assignment statement) for non-string variables,The "= =" and "equals" methods work the sameis used to compare the first address of the object in the heap memory, which is used to compare whether two reference variables point to the same object.

Like what:

classa{A obj1=NewA (); A Obj2=NewA ();} Obj1==obj2//The result is falseobj1.equals (OBJ2)//Is False    //But add this sentence:obj1=Obj2; //after executionobj1==obj2//is Trueobj1.equals (OBJ2)//is True

1. The Equals method is the comparison for a string, and for a non-string comparison, the object it points to is the same.  2, = = The comparison is also the comparison of the object is the same as the object in the memory of the first address. 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 a basic type comparison, then can only use = = to compare, can not use equals such as:
 Public classTestequals { Public Static voidMain (string[] args) {intA = 3; intb = 4; intc = 3; System.out.println (A= = b);//The result is falseSystem.out.println (A = = c);//The result is trueSystem.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 classTestequals { Public Static voidMain (string[] args) {
Integer N1
=NewInteger (30); Integer N2=NewInteger (30); Integer N3=NewInteger (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);//so either the new integer (30) or the new Integer (31) result will show falseSystem.out.println (N1.equals (N2));//The result is true, according to the instructions in the JDK documentation, the contents of the object pointed to by N1 and N2 are equal, both are 30, so the equals comparison results in trueSystem.out.println (N1.equals (N3));//The result is false because the object content is not the same, one is 301 is a} }
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 (thread-safe variable character sequence), StringBuilder ( Variable character sequence) These three classes are described further. (4) This example is an example of the third chapter of Java programming thought:
classValue {inti;}  Public classEQUALSMETHOD2 { Public Static voidMain (string[] args) {Value v1=NewValue (); Value v2=NewValue (); v1.i= V2.I = 100; System.out.println (V1.equals (v2));//(1) flaseSystem.out.println (v1 = = v2);//(2) True} } 
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. Summary: If it is a basic type comparison, then can only use = = to compare, not with equals, if it is the basic type of packaging, then with equals

Usage of equals and = = in Java (brief introduction)

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.