How to use equality and equality (how to use equals)

Difference between equivalence and equivalence

As a part that seems to be incorrect in the Java Silver test result I received the other day "Use == and equals () to test if a string is equal to another object" Was mentioned, so I will write it as a review.

Reference [Introduction to Java that is refreshing](

<div style = "border: 1px solid # 95a5a6; border-radius: .75rem" ; background-color: #FFFFFF; width: 184px; margin: 0px; padding: 5px; text-align: center; overflow: hidden; ">
<a href = "https://hb.afl.rakuten.co.jp/ichiba/1aecd7df.780bbf06.1aecd7e0.a0859005/?pc=https%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F16099007%2F&link_type= picttext & ut = eyJwYWdlIjoiaXRlbSIsInR5cGUiOiJwaWN0dGV4dCIsInNpemUiOiI4MHg4MCIsIm5hbSI6MSwibmFtcCI6InJpZ2h0IiwiY29tIjoxLCJjb21wIjoiZG93biIsInByaWNlIjoxLCJib3IiOjEsImNvbCI6MSwiYmJ0biI6MSwicHJvZCI6MH0% 3D "target =" _ blank "rel =" nofollow sponsored noopener "style =" word-wrap: break-word; "> <img src =" https://hbb.afl.rakuten.co.jp/ hgb / 1aecd7df.780bbf06.1aecd7e0.a0859005 /? me_id = 1213310 & item_id = 19815844 & pc = https% 3A% 2F% 2Fthumbnail.image.rakuten.co.jp% 2F% 400_mall% 2Fbook% 2Fcabinet% 2F7807% 2Fcabinet% 2F7807%2F9784. = 80x80 & t = picttext "border =" 0 "style =" margin: 2px "alt =" [For product price, a link is created Information may change at that time and at this time. ] "title =" [Regarding product prices, information may have changed at the time the link was created and at this time. ] "> </ td>

<a href="https://hb.afl.rakuten.co.jp/ichiba/1aecd7df.780bbf06.1aecd7e0.a0859005/? pc = https% 3A% 2F% 2Fitem.rakuten.co.jp% 2Fbook% 2F16099007% 2F & link_type = picttext & ut = eyJwYWdlIjoiaXRlbSIsInR5cGUiOiJwaWN0dGV4dCIsInNpemUiOiI4MHg4MCIsIm5hbSI6MSwibmFtcCI6InJpZ2h0IiwiY29tIjoxLCJjb21wIjoiZG93biIsInByaWNlIjoxLCJib3IiOjEsImNvbCI6MSwiYmJ0biI6MSwicHJvZCI6MH0% 3D "target =" _ blank "rel =" nofollow sponsored noopener "style =" word-wrap: break-word ; "> A refreshing introduction to Java 3rd edition [Kiyotaka Nakayama]
Price: 2860 yen (tax included, free shipping) </ span> (As of 8/7/2020) </ span> </ p> </ td> </ tr> </ table> </ div>

</ p> </ td> </ tr> </ table>)


public class Human {
  String name;
  int hp;

  public String toString(){
   return "name:" + this.name + "/age:" + this.age;
 }
}

In the above case Equal value

Judgment of (==) "Exactly the same existence" = "pointing to the same address value"

same.java


Human h1 = new Human("Taro")
Human h2 = h1;

Since h1 is assigned to h2, it has the same instance A "name: Taro". The reference destination also refers to the same address at 2112. At this time, "h1 == h2" holds.

Equivalent

equals.java


Human h1 = new Human("Ziro");
Human h2 = new Human("Ziro");

Create each instance → refer to Instance A "name: Ziro" → 3322 Instance B "name: Ziro" → 9191 "H1! = H2" holds However, the contents of the string are the same Therefore, "h1.equalsa (h2)" holds.

Yeah, I understand. But, If you proceed with [Introduction to Java that you can understand clearly]([Introduction to Java that you can understand clearly]) and enter the chapter on API utilization in the latter half It is stated that equals () does not work properly unless the evaluation standard of "what makes it the same" is specified, but it is a little confusing.

The following does not work correctly Example 1) Compare two people

public class Main {
  public static void main(String[] args) {
    Human h1 = new Human();
    h1.name = "Taro";
    h1.age = 10;    

    Human h2 = new Human();
    h2.name = "Taro";
    h2.age = 10;

    if ( h1.equals(h2) == true ) {
      System.out.println("Same");
    } else {
    System.out.println("different") ;
    }
  }
}

The processing content of equals () inherited from Object class is as follows "For the time being, if the values are equal, return true." Will be in the form of


public boolean equals(Object o) {
  if (this == o) { 
    return true; 
} else {
    return false;
  }
}

"What to consider as the same content" is different for each class, so Must be defined for each In other words, specify by "overriding equals"

The condition is "If the names are the same, they are considered to be the same Human" Then it will be as follows


public class Human {
 String name;
 int age;

public boolean equals(Object o ) {
  if (this == o) { 
  return true;
}  if (o instanceof Human) { //Additional overrides below ① Determine if o can be cast to Human type
  Human h = (Human)o;  //(2) Downcast Object type o to Human type and assign it to h
  if ( this.name.equals(h.name)) {
    return true; 
    }  
   }
    return false;
  }
}


"SAME" is displayed

Recommended Posts