= = and Equals () methods in Java

Source: Internet
Author: User

The data types in Java can be divided into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them, applying the double equals sign (= =), compares their values.
2. Reference data type (Class)
When they are compared with (= =), they compare the storage address in memory, so unless it is the same new object, their comparison result is true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals () is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overwritten, such as String,integer, In these classes, date equals has its own implementation, and is no longer the storage address of the comparison class in heap memory.

For the equals comparison between reference data types, the comparison between them is based on the address value of the location in memory where the Equals method is not covered, because the equals method of object is compared with the double equals sign (= =) . So the result of the comparison is the same as the double equals sign (= =).

The Equals () method in Object

public boolean equals (Object obj) {        return (this = = obj);    }

To cite an example:

Person.java

Package Org.java.test;public class Person {private int age;private String name;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public person (int age, String name) {this.age = Age;this.name = name;}  Public person () {} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result + Age;result = Prime * result + ((name = = null)? 0:name.hashcode ()); return result;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return False;if (GetClass ()! = obj . GetClass ()) return false; person other = (person) obj;if (age! = other.age) return false;if (name = = null) {if (other.name! = null) return false;} Els E if (!name.equals (other.name)) return False;return true;}}

Maintest.java

Package Org.java.test;public class Maintest {public static void main (string[] args) {person P1 = new Person ("A"); person P2 = new Person ("A"); Person P3 = P1; System.out.println (P1 = = p2);//falseSystem.out.println (P1 = = p3);//TrueSystem.out.println (P1.equals (p2));// The Equals () method is not overridden, returning false//after overriding the Equals () method, comparing the content to true; System.out.println ("<====================>"); String S1 = "Hello"; String s2 = "Hello"; String s3 = new String ("Hello"); System.out.println (S1 = = s2);//trueSystem.out.println (S1 = = s3);//FalseSystem.out.println (s1.equals (S2));// TrueSystem.out.println (S1.equals (S3));//True}}


Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

= = and Equals () methods 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.