[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item10. equals๋Š” ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์ง€์ผœ ์žฌ์ •์˜ํ•˜๋ผ. (์ถ”์ด์„ฑ, ์ผ๊ด€์„ฑ, null-์•„๋‹˜)

2023. 1. 26. 15:45ใ†JAVA/Effective JAVA

728x90
item10. equals๋Š” ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์ง€์ผœ ์žฌ์ •์˜ํ•˜๋ผ. 

 

equals ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค. 

 

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item10. equals๋Š” ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์ง€์ผœ ์žฌ์ •์˜ํ•˜๋ผ. (๋Œ€์นญ์„ฑ, ๋ฐ˜์‚ฌ์„ฑ, ์ถ”์ด์„ฑ) (tistory.com)

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item10. equals๋Š” ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์ง€์ผœ ์žฌ์ •์˜ํ•˜๋ผ. (๋Œ€์นญ์„ฑ, ๋ฐ˜์‚ฌ์„ฑ, ์ถ”์ด์„ฑ)

item10. equals๋Š” ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์ง€์ผœ ์žฌ์ •์˜ํ•˜๋ผ. equals ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค. equals ๊ทœ์•ฝ 1๏ธโƒฃ ๋ฐ˜์‚ฌ์„ฑ reflexivity : null ์ด ์•„๋‹Œ ๋ชจ๋“  ์ฐธ์กฐ ๊ฐ’ x์— ๋Œ€ํ•ด x.equals(x) == true

hyejin.tistory.com

 

equals ์ผ๋ฐ˜ ๊ทœ์•ฝ 

1๏ธโƒฃ ๋ฐ˜์‚ฌ์„ฑ

A.equals(A) == true 

 

2๏ธโƒฃ ๋Œ€์นญ์„ฑ

A.equals(B) == B.equals(A) 

 

3๏ธโƒฃ ์ถ”์ด์„ฑ

A.equals(B) == B.equals(C) , A.equals(C) 

 

๊ตฌ์ฒด ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•ด ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉด์„œ equals ๊ทœ์•ฝ์„ ๋งŒ์กฑ์‹œํ‚ฌ ๋ฐฉ๋ฒ•์€ ์กด์žฌํ•˜์ง€ ์•Š์ง€๋งŒ ๊ดœ์ฐฎ์€ ์šฐํšŒ ๋ฐฉ๋ฒ•์ด ํ•˜๋‚˜ ์žˆ๋‹ค. 

-> " ์ƒ์† ๋Œ€์‹  ์ปดํฌ์ง€์…˜์„ ์‚ฌ์šฉํ•˜๋ผ!" ์ธ๋ฐ Point๋ฅผ ์ƒ์†ํ•˜๋Š” ๋Œ€์‹ ์— Point๋ฅผ ColorPoint์˜ private ํ•„๋“œ๋กœ ๋‘๊ณ , ColorPoint์™€ ๊ฐ™์€ ์œ„์น˜์˜ ์ผ๋ฐ˜ Point๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ทฐ ๋ฉ”์„œ๋“œ๋ฅผ public ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ์‹์ด๋‹ค. 

 

// equals ๊ทœ์•ฝ์„ ์ง€ํ‚ค๋ฉด์„œ ๊ฐ’ ์ถ”๊ฐ€ํ•˜๊ธฐ -> ์ƒ์† ๋Œ€์‹  ์ปดํฌ์ง€์…˜์„ ์‚ฌ์šฉํ•˜๋ผ!
public class ColorPoint
{
   private final Point point;
   
   private final Color color;
   
   public ColorPoint(int x, int y,  Color color)
   {
      this.point = new Point(x, y);
      this.color = Objects.requireNonNull(color);
   }
   
   // colorPoint์˜ Point ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
   public Point asPoint()
   {
      return point;
   }
   
   @Override
   public boolean equals(Object obj)
   {
      if (!(obj instanceof ColorPoint)) {
         return false;
      }
      
      ColorPoint cp = (ColorPoint) obj;
      return cp.point.equals(point) && cp.color.equals(color);
   }
   
   @Override
   public int hashCode()
   {
      return 31 * point.hashCode() + color.hashCode();
   }
}
    public static void main(String[] args)
   {
      Point p1 = new Point(1, 0);
//    Point p2 = new CounterPoint(1, 0);
      Point p2 = new ColorPoint(1, 0, Color.RED).asPoint();
      
      System.out.println("onUnitCircle(p1) = " + onUnitCircle(p1)); // true ์ถœ๋ ฅ
      
      // true๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜์ง€๋งŒ, Point์˜ equals๊ฐ€ getClass๋ฅผ ์‚ฌ์šฉํ•ด ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋ฉด false๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
      System.out.println("onUnitCircle(p2) = " + onUnitCircle(p2));
   }

์ปดํฌ์ง€์…˜์œผ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  asPoint๋ฅผ ํ†ตํ•ด Point๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋กํ•˜๋ฉด  onUnitCirCle(p2)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•ด๋ณด๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

4๏ธโƒฃ ์ผ๊ด€์„ฑ 

: ์ผ๊ด€์„ฑ์€ ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™๋‹ค๋ฉด(์–ด๋Š ํ•˜๋‚˜ ํ˜น์€ ๋‘ ๊ฐ์ฒด ๋ชจ๋‘ ์ˆ˜์ •๋˜์ง€ ์•Š๋Š” ํ•œ) ์•ž์œผ๋กœ๋„ ์˜์›ํžˆ ๊ฐ™์•„์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. 

๊ฐ€๋ณ€ ๊ฐ์ฒด๋Š” ๋น„๊ต ์‹œ์ ์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅผ ์ˆ˜๋„ ํ˜น์€ ๊ฐ™์„ ์ˆ˜๋„ ์žˆ๋Š” ๋ฐ˜๋ฉด์— ๋ถˆ๋ณ€ ๊ฐ์ฒด๋Š” ํ•œ๋ฒˆ ๋‹ค๋ฅด๋ฉด ๋๊นŒ์ง€ ๋‹ฌ๋ผ์•ผ ํ•œ๋‹ค. 

 

ํด๋ž˜์Šค๊ฐ€ ๋ถˆ๋ณ€์ด๋“  ๊ฐ€๋ณ€์ด๋“  equals ํŒ๋‹จ์— ์‹ ๋ขฐํ•  ์ˆ˜ ์—†๋Š” ์ž์›์ด ๋ผ์–ด๋“ค๊ฒŒ ํ•ด์„œ๋Š” ์•ˆ๋˜๋Š”๋ฐ ์ด ์ œ์•ฝ์„ ์–ด๊ธฐ๋ฉด ์ผ๊ด€์„ฑ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ์–ด๋ ต๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด java.net.URL์˜ equals๋Š” ์ฃผ์–ด์ง„ URL๊ณผ ๋งคํ•‘๋œ ํ˜ธ์ŠคํŠธ์˜ IP ์ฃผ์†Œ๋ฅผ ์ด์šฉํ•ด ๋น„๊ตํ•œ๋‹ค.

ํ˜ธ์ŠคํŠธ ์ด๋ฆ„์„ ip์ฃผ์†Œ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ํ•ญ์ƒ ๊ฐ™๋‹ค๊ณ  ๋ณด์žฅํ•  ์ˆ˜ ์—†๋‹ค. 

// ์ผ๊ด€์„ฑ ์œ„๋ฐฐ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ.
// URL equals๋Š” ์ฃผ์–ด์ง„ URL๊ณผ ํ˜ธ์ŠคํŠธ์˜ ip ์ฃผ์†Œ๋ฅผ ์ด์šฉํ•ด ๋น„๊ตํ•œ๋‹ค.
URL google1 = new URL("https", "about.google", "/products");
URL google2 = new URL("https", "about.google", "/products");
System.out.println("google1.equals(google2) = " + google1.equals(google2));

์ด๋Š” URL์˜ equals๊ฐ€ ์ผ๋ฐ˜ ๊ทœ์•ฝ์„ ์–ด๊ธฐ๊ฒŒ ํ•˜๊ณ , ์‹ค๋ฌด์—์„œ๋„ ์ข…์ข… ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚จ๋‹ค. 

์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๋ ค๋ฉด equals๋Š” ํ•ญ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ๊ฐ์ฒด๋งŒ์„ ์‚ฌ์šฉํ•œ ๊ฒฐ์ •์  ๊ณ„์‚ฐ๋งŒ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค. 

 

 

5๏ธโƒฃ null-์•„๋‹˜

๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ null๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

728x90