CH11 – The equals Method

  1. Create a new class named Person.
  2. Add the following code.
public class Person {
  public int age;
  public String name;
  public Person(int a, String b){
    this.age = a;
    this.name = b;
  }
  public boolean equals(Person thePerson){
    if(thePerson.age == this.age && this.name.equals(thePerson.name))
    {
      return true;
    }
    return false;
  }
  public static void main(String[] args){
    Person a = new Person(14,"James");
    Person b = new Person(14,"James");
    System.out.println("a==b " + (a==b));
    System.out.println(a.equals(b));
  }
}
  1. Compile and run the program.
$ javac Person.java
$ java Person
a==b false
true