I'm a little confused as to what gets called first in the following scenario:
I've got a Person class with a name and an age.
public class Person {
private int age;
private String name;
//constructor, getters, ...
}
I now try to compare two separate Person objects with the same field values i.e. same name, same age
System.out.println(person1.equals(person2));
I obviously receive a false return value because I haven't overriden the equals(Obj object) method yet.
But say I don't override it, I instead write another equals() method only with a Person argument:
public equals(Person person) {
return person.get_age()==this.age && person.get_name().equals(this.name);
}
Which method gets called now and why? The line below still returns false
System.out.println(person1.equals(person2));
unless I force person2 to be interpreted as a Person. The line below therefore returns true
System.out.println(person1.equals((Person)person2));
Here's what's confusing me, I now remove that type cast to Person and for some reason, when I build and run the code again and again, it keeps coming back true. The only way to force it to start returning false again, i.e. to keep taking the default equals(Object obj) method again is by deleting the equals(Person person) method, running the code once, and then pasting back the method I had just deleted. Now the comparison keeps returning false again.
Is some inbetween state being cached by the JVM? I'm a little new to java in all honesty.