The difference between equals and = = and Comparato in Java

Source: Internet
Author: User
Tags comparable

First talk about the difference between equals and = = in Java:


The data types in Java can be divided into two categories:

1. Basic data type (also called raw data type)

Eight basic data types char byte short int long double float Boolean

2. Reference data type (also composite data type) except for the 8 above, the other is the reference data type

Difference:

The base data type comparison can only be used = =, not equals, because with equals (argument), the argument must be an object.

The comparison between them is their value.

 Public class Test {    publicstaticvoid  main (string[] args) {        int A = 1;         int b = 1;         = = b);}    }

When a reference type is compared with (= =)
The comparison is that they store the address in memory, unless the same address, otherwise the result is false.

  Class   Test { public  static  void   main (string[] args) {A A1        /span>= new   A ();        A a2  = new   A (); SYSTEM.OUT.PRINTLN (a1  = = A2); //                A A3 = A1; SYSTEM.OUT.PRINTLN (a1  = = A3); // true   }}    

  All classes in Java are inherited from the base class of object, and the Equals method is defined in object, and the initial behavior of this method is to compare the memory address of the object (as in the example above), because the Equals method of object is compared with the double equals sign (= =). So the result of comparison is the same as the result of double equal sign (= =).

The Equals method inside the object:

     Public Boolean equals (Object obj) {        return ( this = = obj);    }

However, in some classes this method is rewritten, such as String,integer, the overridden equals is no longer the object of comparison in memory storage address, but rather the content of the comparison.

In the case of string, the Equals method code is:

   Public Booleanequals (Object anobject) {if( This==anobject) {            return true; }        if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =value.length; if(n = =anotherString.value.length) {CharV1[] =value; CharV2[] =Anotherstring.value; inti = 0;  while(n--! = 0) {                    if(V1[i]! =V2[i])return false; I++; }                return true; }        }        return false; }

As an example:

 Public classTest { Public Static voidMain (string[] args) {Integer a=NewInteger (1); Integer b=NewInteger (1); System.out.println (A.equals (b));//trueString S1=NewString ("ABC"); String S2=NewString ("ABC"); System.out.println (s1.equals (S2));//true    }}

The above example is purely comparative, and can be changed if the other code is added to the middle.

Two. Compare the differences between Comparato and equals in string

only the class that implements the comparable has the Comparato method, and the string implements the comparable interface class, so CompareTo is the method of string.

public class Test {
public static void Main (string[] args) {
string S1 = new String ("abc");
String s2 = new String ("abc");
System.out.println (s1.equals (S2));//True
System.out.println (S1.compareto (S2));//0

String s3 = new String ("abc");
String S4 = new String ("ABCA");
String S5 = new String ("ABCDE");
String s6 = new String ("ABCD");


System.out.println (S3.compareto (S4));//-1
System.out.println (S5.compareto (S6));//1


System.out.println (S5.compareto (S4));//3
System.out.println (S4.compareto (S5));//-3
}
}

 Difference: 

Usually the CompareTo is used to compare the size and equals is the comparison equal.

    Under the year, equals is efficient because CompareTo is comparing two strings in dictionary order. That is, the Unicode values of each character in the string are compared sequentially.

When CompareTo:

When the same time: Returns int 0, when less than, if this bit is empty, then returns-1, otherwise the Unicode difference is returned, if the parameter corresponding bit is empty, then 1, otherwise the difference.

The difference between equals and = = and Comparato in Java

Related Article

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.