<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sauvé Jean-Luc</title>
    <description>The latest articles on DEV Community by Sauvé Jean-Luc (@sauve2003).</description>
    <link>https://dev.to/sauve2003</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F547382%2F18eed07c-d447-4b26-ae84-fdb9c7d641c5.png</url>
      <title>DEV Community: Sauvé Jean-Luc</title>
      <link>https://dev.to/sauve2003</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sauve2003"/>
    <language>en</language>
    <item>
      <title>Why we need to override equals method in Java</title>
      <dc:creator>Sauvé Jean-Luc</dc:creator>
      <pubDate>Wed, 07 Apr 2021 09:44:12 +0000</pubDate>
      <link>https://dev.to/sauve2003/why-we-need-to-override-equals-method-in-java-1ff5</link>
      <guid>https://dev.to/sauve2003/why-we-need-to-override-equals-method-in-java-1ff5</guid>
      <description>&lt;p&gt;By default, when we compare two instances of the same class in Java, it is checked whether both refer to the same object or not.&lt;/p&gt;

&lt;p&gt;Therefore, two instances may be completely different regardless of the similarity of the information contained. Take for Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class PersonDetails {

    private Integer age;
    private String firstName;
    private String lastName;
    private String nationality;

    public PersonDetails(Integer age, String firstName, String lastName, String nationality) {
        this.age = age;
        this.firstName = firstName;
        this.lastName = lastName;
        this.nationality = nationality;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the main class as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args){
        PersonDetails first = new PersonDetails(18,"John","Doe","American");
        PersonDetails second = new PersonDetails(18,"John","Doe","American");

        if(first.equals(second)){
            System.out.println("Equal");
        }
        else {
            System.out.println("Not equal");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be: &lt;strong&gt;Not equal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And for us to check for equality of values inside the objects, we will need to override the equals method in our class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class PersonDetails {

        private Integer age;
        private String firstName;
        private String lastName;
        private String nationality;

        public PersonDetails(Integer age, String firstName, String lastName, String nationality) {
            this.age = age;
            this.firstName = firstName;
            this.lastName = lastName;
            this.nationality = nationality;
        }

        //Overriding equals() to compare PersonDetails objects
        @Override
        public boolean equals(Object o){
                //If object is compared to itself then return
                if(o == this){
                    return true;
                }

                //Check if o is an instance of PersonDetails or not
                if(!(o instanceof  PersonDetails)){
                    return false;
                }

                // typecast o to PersonDetails so that we can compare data
                PersonDetails personDetails = (PersonDetails) o;

                // Compare the values inside the objects and return accordingly
                return Integer.compare(age,personDetails.age) == 0
                        &amp;amp;&amp;amp; firstName.compareTo(personDetails.firstName) == 0
                        &amp;amp;&amp;amp; lastName.compareTo(personDetails.lastName) ==0
                        &amp;amp;&amp;amp; nationality.compareTo(personDetails.nationality) == 0;

        }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run the main class again, &lt;br&gt;
The output will be: &lt;strong&gt;Equal&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Get source code used in this article from GitHub&lt;br&gt;
&lt;a href="https://github.com/SauveJeanLuc/TestEqualsAndHashcode"&gt;Link&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>springboot</category>
      <category>spring</category>
    </item>
  </channel>
</rss>
