-4

I'm learning about == vs. equals() and doing various examples. For this one, I understand why r==s is false, but why is r.equals(s) false if they now both have the same content?

public class StringProgram{
  public static void main(String[] args) {

    Person r = new Person("A");
    Person s = new Person("J");
    s.setName("A");

    System.out.println(r.getName());//A
    System.out.println(s.getName());//A
    System.out.println(r==s);//false
    System.out.println(r.equals(s));//false


  }
}

Here is the code of Person :

public class Person{
    private String name;

    public Person(String d){
    name=d;
    }
    public void setName(String a){
      name=a;
    }

    public String getName(){
      return name;
    }
}

EDIT: I see that I have to override it now, but I still don't understand how the assignment works in the example below. I just don't understand why t.getName() is Keen if t is assigned to u.

public class StringProgram{ public static void main(String[] args) {

Person t = new Person("Gene");
Person u = t;
u.setName("Keen");

System.out.println(t.getName());//Keen
System.out.println(t.equals(u));//true

} }

  • possible duplicate of [Java == vs equals() confusion](http://stackoverflow.com/questions/7520432/java-vs-equals-confusion) – pathfinderelite May 23 '15 at 21:49
  • 1
    How could we know without the code of `Person` ? – Dici May 23 '15 at 21:49
  • @pathfinderelite you can't say it's a duplicate before the question has been clarified – Dici May 23 '15 at 21:50
  • public class Person{ private String name; public Person(String d){ name=d; } public void setName(String a){ name=a; } public String getName(){ return name; } – user4932934 May 23 '15 at 21:50
  • 2
    You need to override [`equals`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)) in `Person`. – Mick Mnemonic May 23 '15 at 21:51
  • The deal with the u and t question is that u and t are both references, and when u is assigned to t (`u = t`) they both refer to the same object. So changing the state of u's object changes the object t points to as well. – D. Ben Knoble May 23 '15 at 22:36

4 Answers4

0

equals() is just a method, there's nothing special about it. In particular, it doesn't automatically know how to compare the content of two objects.

The equals() method of class Object does the same thing as ==. If you do not override the equals() method in your class Person, then it's not automatically going to compare the content of Person objects.

You have to override the equals() method in class Person so that it does the comparison in the way you want.

public class Person {
    private String name;

    // ...

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Person)) {
            return false;
        }

        return ((Person) o).name.equals(this.name);
    }
}
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

All classes in java inherit from the Object class. When you do an r.equals(s), it uses the .equals() method as defined in Object class. To make it work as you desire, you need to define the .equals() method for the Person class by overriding the .equals() method.

Eg. Add this in your Person class:

    @Override
    public 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 (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

Note: I used eclipse to generate the equals and hashcode method for me. You may read this thread to understand why hashcode() needs to be overridden.

Community
  • 1
  • 1
Bajal
  • 5,487
  • 3
  • 20
  • 25
0

Java can't guess what you want to do, you have to tell it that two persons are the same if they have the same name. You must override equals :

@Override
public boolean equals(Object o) {
   if (o == this) return true;
   if (o == null) return false;
   if (!(o instanceof Person)) return false;
   Person that = (Person) o;

   if (name == null && that.name == null) return true;
   if (name == null || that.name == null) return false;
   return that.name.equals(name);
}
Dici
  • 25,226
  • 7
  • 41
  • 82
0

All java classes inherits from Object, and to properly compare your class you should override the equals() method.

public Person(String d){
name=d;
}
public void setName(String a){
  name=a;
}

public String getName(){
  return name;
}

@Override
public boolean equals(Object obj) {
  return obj instanceof Person && 
         this.getName().equals(((Person)obj).getName());
}
letz
  • 1,762
  • 1
  • 20
  • 40