The difference between java equals and = =

Source: Internet
Author: User
Tags date1

It probably says equals and = = Compare What:

1. Boolean tem = a = = B;

The first = = comparison is definitely the address, from the stack point of view that is = = Compare the contents of the stack above. Because the stack is used to store the literal value of an automatic variable in an address or eight basic types in Java (an automatic variable is a variable defined with an int a = 1; this form). If it is an automatic variable comparison, it must be compared with = =, because Equals () is a method, so it must be called by the object to be used for comparison. The Equals () method cannot be used because an automatic variable is neither an instance of a class nor a reference to a class.

2.boolean tem = a.equals ("B");

The Equals () method is typically used to compare the contents of an object, but in some cases it also compares the addresses of two objects.

Next

Write a test program

Package com;

Import Java.util.Date;

Public class Test {
Public static voidMain (string[] args) {
Integer integer1 =New Integer (1);
Integer integer2 =New Integer (1);
String str1 =New String ("123");
String str2 =New String ("123");
Date date1 =new Date ();
Date date2 =new Date ();
Double double1 =New Double ("1.0");
Double double2 =New Double ("1.0");
Boolean tem1 =New Boolean (true);
Boolean tem2 =New Boolean (true);
Object Object1 =new Object ();
Object object2 =new Object ();

System.Out.println ("----Object------");
System.out.println (Object1.equals (object2));
System.out.println (Object1 = = object2);
System.out.println (Object1.equals (object1));
System.out.println (Object1 = = Object1);
System.Out.println ("----Boolean------");
System.out.println (Tem1.equals (tem2));
System.out.println (tem1 = = tem2);
System.Out.println ("----Double------");
System.out.println (Double1.equals (double2));
System.out.println (Double1 = = double2);
System.Out.println ("----Integer------");
System.out.println (Integer1.equals (integer2));
System.out.println (Integer1 = = Integer2);
System.Out.println ("----String------");
System.out.println (Str1.equals (str2));
System.out.println (str1 = = str2);
System.out.println ( "----Date------")
System. out.println (Date1.equals (DATE2)
System. out.println (date1 = date2)
}
}
/span>

Results:

----Object------
False
False
True
True
----Boolean------
True
False
----Double------
True
False
----Integer------
True
False
----String------
True
False
----Date------
True
False

First compared to object, when the two objects are compared, the result of = = and Equals () is true, and when two objects are different, the return is false. So when the = = and equals are used to compare objects, they are compared to the address of the object, in fact, the same nature. The following is the code for the Equals () method in the object class:

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

  

And for boolean,double (float the same), Interger (Shot,long same), string,date I also found their source code, the following I pasted out in turn in boolean,double,interger,string, Date the source of the Equals () method in these classes, this time the Equals () method is rewritten, because these classes are inherited from the object class.

Boolean:

 Public Boolean equals (Object obj) {        ifinstanceof  Boolean) {            return value = = ( (Boolean) obj). Booleanvalue ();        }         return false ;    }

Double:

 Public Boolean equals (Object obj) {        returninstanceof  Double)               && ( Doubletolongbits ((Double) obj). value) = =                      doubletolongbits (value));    

Interger:

 Public Boolean equals (Object obj) {        ifinstanceof  Integer) {            return value = = ((Integer) obj). Intvalue ();        }         return false ;    }

String:

 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; }

Date:

 Public Boolean equals (Object obj) {        returninstanceof Date && getTime () = = ((Date) obj ). GetTime ();    }

That is to say that at these times the Equals () method of the object class is used to compare the actual contents of two objects and no longer addresses, certainly not just these, but here are just a few of the more common Java native classes overriding the Equals () method of the object class.

  

The difference between java equals and = =

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.