title: How to override equals method in Java
body: <p>I am trying to override equals method in Java. I have a class <code>People</code> which basically has 2 data fields <code>name</code> and <code>age</code>. Now I want to override <code>equals</code> method so that I can check between 2 People objects. </p>
  
  <p>My code is as follows</p>
  
  <pre><code>public boolean equals(People other){
      boolean result;
      if((other == null) || (getClass() != other.getClass())){
          result = false;
      } // end if
      else{
          People otherPeople = (People)other;
          result = name.equals(other.name) &amp;&amp;  age.equals(other.age);
      } // end else
  
      return result;
  } // end equals
  </code></pre>
  
  <p>But when I write <code>age.equals(other.age)</code> it gives me error as equals method can only compare String and age is Integer. </p>
  
  <h2>Solution</h2>
  
  <p>I used <code>==</code> operator as suggested and my problem is solved.</p>