๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Today I Learned(TIL)/์„ธ์…˜

Java ์„ธ์…˜_๋น„๊ต๋ž€ ๋ฌด์—‡์ผ๊นŒ? (๋™๋“ฑ์„ฑ๊ณผ ๋™์ผ์„ฑ)

by carrot0911 2024. 11. 22.

๋™๋“ฑ์„ฑ / ๋™์ผ์„ฑ ์„ธ์…˜ (๊ฐ์ฒด์™€ ๋ฐ์ดํ„ฐ ๋น„๊ต)

11/21 15:00 ~ 16:00 (์•ฝ 1์‹œ๊ฐ„ ์ง„ํ–‰)

 

์‹œ๋‚˜๋ฆฌ์˜ค

  • ์ฑ… 3๊ฐœ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด ์ค‘์—์„œ ๊ฐ™์€ ์ฑ…์ด ๋ฌด์—‡์ธ์ง€ ์ฐพ์•„๋‚ด์•ผ ํ•œ๋‹ค.
  • ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ํ†ตํ•ด์„œ ์ž๋ฐ”์—์„œ ๋น„๊ต๋ž€ ๊ฐœ๋…์ด ๋ฌด์—‡์ด๊ณ  ๋” ๋‚˜์•„๊ฐ€์„œ ๋™๋“ฑ์„ฑ๊ณผ ๋™์ผ์„ฑ์˜ ์ฐจ์ด์ ์„ ์•Œ์•„๋ณด์ž.

 

์‹ค์Šต

๋”๋ณด๊ธฐ

Book.java

public class Book {

    private String name;
    private String author;

    public Book(String name, String author) {
        this.name = name;
        this.author = author;
    }
}

Main.java

public class Main {

    public static void main(String[] args) {
        int a = 1;
        int b = 1;
        int c = 2;

        Book bookA = new Book("์†Œ๋…„์ด์˜จ๋‹ค", "ํ•œ๊ฐ•");
        Book bookB = new Book("์†Œ๋…„์ด์˜จ๋‹ค", "ํ•œ๊ฐ•");
        Book bookC = new Book("์ฑ„์‹์ฃผ์˜์ž", "ํ•œ๊ฐ•");
    }
}

 

๋ฌธ์ œ ์ƒํ™ฉ

public class Main {

    public static void main(String[] args) {
    
        ...
        
        boolean ret = (bookA == bookB);
        System.out.println("ret = " + ret); // false ์ถœ๋ ฅ
    }
}

๋น„๊ต์—ฐ์‚ฐ์ž(==)๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋น„๊ต๋ฅผ ํ•ด๋ณด๋ฉด ๊ฐ™์€ ์ฑ…์ด์ง€๋งŒ ๋‹ค๋ฅธ ์ฑ…์ด๋ผ๊ณ  false๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

public class Main {

    public static void main(String[] args) {
        int a = 1;
        int b = 1;
        int c = 2;
        
        ...
        
        boolean ret2 = (a == b);
        System.out.println("ret2 = " + ret2); // true ์ถœ๋ ฅ
    }
}

๋ณ€์ˆ˜ a์™€ b๋ฅผ ๋น„๊ตํ•ด ๋ณด๋ฉด true๊ฐ€ ์ž˜ ์ถœ๋ ฅ๋˜๋Š” ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ..?

 

๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ์™€ ๋ณ€์ˆ˜

  • Method Area
    • .class ํŒŒ์ผ์˜ ์ •๋ณด๊ฐ€ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ณณ์ด๋‹ค.
    • ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ ๋ชจ๋“  ํด๋ž˜์Šค์™€ ๋ฉ”์„œ๋“œ ์ •๋ณด(ํด๋ž˜์Šค ๊ตฌ์กฐ, ๋ฉ”์„œ๋“œ ์ •๋ณด, ์ƒ์ˆ˜ ๋“ฑ)๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ์ด๋‹ค.
  • Stack Area
    • FILO(First In Last Out, ์„ ์ž…ํ›„์ถœ) ๊ตฌ์กฐ์ด๋‹ค.
    • ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ๋ฉ”์„œ๋“œ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง„ ์ƒˆ๋กœ์šด ์Šคํƒ ํ”„๋ ˆ์ž„์ด ์Œ“์ธ๋‹ค.
    • ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ ‘์‹œ๋ฅผ ๋จผ์ € ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜๋“ฏ์ด ๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.
  • Heap Area
    • ํ•„์š”์— ๋”ฐ๋ผ ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณต๊ฐ„์ด๋‹ค.

๊ทธ๋ž˜์„œ ์„ ์–ธํ•œ book ๊ฐ์ฒด๋“ค์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ฃผ์†Œ๊ฐ’์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค.

 

โœ”๏ธ ์ฐธ๊ณ  - ์ฐธ์กฐ ๋ณ€์ˆ˜ (referenceType)

๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์˜ ์œ„์น˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐ๋ณ€์ˆ˜( referenceType)๋ผ๊ณ  ํ•œ๋‹ค.
public static void main(String[] args) {
    int a = 1;
    int b = 1;
    int c = 2;

    Book bookA = new Book("์†Œ๋…„์ด์˜จ๋‹ค", "ํ•œ๊ฐ•");
    Book bookB = new Book("์†Œ๋…„์ด์˜จ๋‹ค", "ํ•œ๊ฐ•");
    Book bookC = new Book("์ฑ„์‹์ฃผ์˜์ž", "ํ•œ๊ฐ•");

    System.out.println(a); // 1
    System.out.println(b); // 1
    System.out.println(c); // 2
    System.out.println(bookA); // equal.Book@4517d9a3
    System.out.println(bookB); // equal.Book@372f7a8d
    System.out.println(bookC); // equal.Book@2f92e0f4
}

== ์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ ํƒ€์ž…์—์„œ๋Š” ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ , ์ฐธ์กฐ ํƒ€์ž…์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ(์ฐธ์กฐ)๋ฅผ ๋น„๊ตํ•œ๋‹ค.

 

๋™๋“ฑ์„ฑ๊ณผ ๋™์ผ์„ฑ

๋™๋“ฑ์„ฑ(Equality)

๋™๋“ฑ์„ฑ์€ .equals( ) ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๋™์ผ์„ฑ(Identity)

๋™์ผ์„ฑ์€ == ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ๊ฐ’์œผ๋กœ ํŒ๋‹จํ•œ๋‹ค.

 

Equals ์‚ฌ์šฉ

boolean ret = (bookA.equals(bookB));
System.out.println("ret = " + ret); // false

.equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ์™œ false๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ธ๊ฐ€........

 

Equals์˜ ๋น„๋ฐ€

public boolean equals(Object obj) {
    return (this == obj)
}
โœ”๏ธ ์ฐธ๊ณ  - equals( )๋ฅผ ์–ธ์ œ๋“ ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ 

Object ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค.
๋ชจ๋“  ๊ฐ์ฒด๋Š” Object ๊ฐ์ฒด์˜ ์ž์†์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋ถ€๋ชจ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!


โญโญโญํ€ด์ฆˆ - ๋ฉด์ ‘์—์„œ ๋‚˜์˜ค๋Š” ์งˆ๋ฌธ
๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒƒ → overriding

 

์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ์ด์šฉํ•œ ์ง์ ‘ ๊ตฌํ˜„ (๋น„์ถ”์ฒœ)

@Override
public boolean equals(Object object) {
    Book book = (Book) object;
    if (this.name == book.getName() && this.author == book.getAuthor())  {
        return true;
    } else {
        return false;
    }
}

์ง์ ‘ ๋งŒ๋“ค ๊ฒฝ์šฐ ์ž ์žฌ์  ์˜ค๋ฅ˜ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ์ƒ๊ฒจ์„œ ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ธํ…”๋ฆฌ์ œ์ด๋ฅผ ํ†ตํ•ด ์ง์ ‘ ๊ตฌํ˜„ (์ถ”์ฒœ)

@Override
public boolean equals(Object object) {
    if (this == object) return true;
    if (object == null || getClass() != object.getClass()) return false;
    Book book = (Book) object;
    return Objects.equals(name, book.name) && Objects.equals(author, book.author);
}

 

 

ํ€ด์ฆˆ
hashCode( )์˜ ์ •์ฒด๋Š” ๋ฌด์—‡์ผ๊นŒ?

equals( ) ๋ฉ”์„œ๋“œ์—๊ฒŒ๋Š” ๋‹จ์ง์นœ๊ตฌ hashCode( )๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๊ฐ€ equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋ฉด ํ•ญ์ƒ hashCode( )์™€ ํ•จ๊ป˜ ์ •์˜ํ•ด ์ค˜์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”?!

1. Java์˜ ๊ฐ์ฒด ๋น„๊ต์™€ ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜

Java์—๋Š” ๊ฐ์ฒด์˜ ๋™์ผ์„ฑ์„ ํŒ๋‹จํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • ๋™์ผ์„ฑ(Identity) : == ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๋Š”์ง€ ๋น„๊ตํ•œ๋‹ค.
  • ๋™๋“ฑ์„ฑ(Equality) : equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐ์ฒด๊ฐ€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•œ์ง€๋ฅผ ํŒ๋‹จํ•œ๋‹ค.

equals( ) ๋ฉ”์„œ๋“œ๋ฅผ override ํ•˜๋ฉด, ๊ฐ์ฒด๊ฐ€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•œ์ง€๋ฅผ ์šฐ๋ฆฌ์˜ ๊ธฐ์ค€๋Œ€๋กœ ์ •์˜ํ•˜๊ฒŒ ๋œ๋‹ค.

ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜(ex: HashSet, HashMap)์˜ ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์ž.
์ปฌ๋ ‰์…˜๋“ค์€ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด๋“ค์„ ์ €์žฅํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•œ๋‹ค. Java์—์„œ ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ”๋กœ hashCode( )์ด๋‹ค.

2. equals( )์™€ hashCode( )์˜ ๊ณ„์•ฝ

equals( )์™€ hashCode( )๋Š” ํŠน๋ณ„ํ•œ ๊ณ„์•ฝ์„ ๋งบ๊ณ  ์žˆ๋‹ค.

  • ๋™๋“ฑํ•œ ๋‘ ๊ฐ์ฒด๋Š” ๋™์ผํ•œ ํ•ด์‹œ์ฝ”๋“œ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.
    • x.equals(y) == true๋ผ๋ฉด, ๋ฐ˜๋“œ์‹œ x.hashCode( ) == y.hashCode( )์—ฌ์•ผ ํ•œ๋‹ค.

์ด ๊ณ„์•ฝ์ด ์ค‘์š”ํ•œ ์ด์œ ๋Š” ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์ด ํ•ด์‹œ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด์˜ ์ €์žฅ ์œ„์น˜๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, HashMap์ด๋‚˜ HashSet ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๊ฒ€์ƒ‰ํ•œ๋‹ค.

  1. ๊ฐ์ฒด์˜ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ์–ด๋Š ๋ฒ„ํ‚ท์— ์ €์žฅํ• ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
  2. ๊ทธ ๋ฒ„ํ‚ท์— ์ €์žฅ๋œ ๊ฐ์ฒด๋“ค๊ณผ equals( )๋กœ ๋น„๊ตํ•˜์—ฌ ๋™์ผํ•œ ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

๋งŒ์•ฝ equals( )๋ฅผoverride ํ•˜๋ฉด์„œ hashCode( )๋ฅผ override ํ•˜์ง€ ์•Š์œผ๋ฉด, ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๋‘ ๊ฐ์ฒด๋ผ๋„ ๋‹ค๋ฅธ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

3. ์™œ HashCode( )๋ฅผ override ํ•ด์•ผ ํ•˜๋‚˜์š”?

equals( )๋ฅผ override ํ•˜๊ฒŒ ๋˜๋ฉด ๋‘ ๊ฐ์ฒด๊ฐ€ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋Š” ๊ธฐ์ค€์„ ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜๋“ค์€ ์ด ๋…ผ๋ฆฌ์  ๋™์ผ์„ฑ์— ๋งž์ถฐ์„œ ๋™์ž‘ํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋™๋“ฑํ•œ ๊ฐ์ฒด๋Š” ๋ฐ˜๋“œ์‹œ ๋™์ผํ•œ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” ๊ทœ์น™์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด hashCode( )๋„ ํ•จ๊ป˜ override ํ•ด์•ผ ํ•œ๋‹ค.

๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ

public class Person {
    private String name;
    private int age;

    // equals() ์˜ค๋ฒ„๋ผ์ด๋“œ
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }

    // hashCode() ์˜ค๋ฒ„๋ผ์ด๋“œ
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

equals( )๋Š” name๊ณผ age๊ฐ€ ๊ฐ™์œผ๋ฉด ๋™์ผํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ , hashCode( )๋Š” name๊ณผ age๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด HashSet์— ๊ฐ์ฒด๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ ๊ฒ€์ƒ‰ํ•  ๋•Œ, ๊ฐ™์€ ๋…ผ๋ฆฌ์  ๊ฐ’์„ ๊ฐ€์ง„ ๊ฐ์ฒด๋Š” ํ•ญ์ƒ ๊ฐ™์€ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ HashSet์€ ์ค‘๋ณต๋œ ๊ฐ์ฒด๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

4. hashCode( )๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์˜ ๋ฌธ์ œ์ 

hashCode( )๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š์œผ๋ฉด, ๊ธฐ๋ณธ์ ์œผ๋กœ Object ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Š” ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๊ฐ์ฒด๋ผ๋„ ์„œ๋กœ ๋‹ค๋ฅธ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. ์ด๋กœ ์ธํ•ด ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์—์„œ๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๋Œ€๋กœ ๊ด€๋ฆฌํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

์š”์•ฝ

  • equals( )๋ฅผ override ํ•˜๋ฉด ๋…ผ๋ฆฌ์  ๋™์ผ์„ฑ์„ ์ •์˜ํ•˜๊ฒŒ ๋œ๋‹ค.
  • hashCode( )๋ฅผ ํ•จ๊ป˜ override ํ•˜์ง€ ์•Š์œผ๋ฉด, ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๊ฐ์ฒด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์„œ ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜์—์„œ ์˜๋„ํ•œ ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ equals( )์™€ hashCode( )๋Š” ํ•ญ์ƒ ํ•จ๊ป˜ override ํ•ด์•ผ ํ•˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ํ•ด์‹œ ๊ธฐ๋ฐ˜ ์ปฌ๋ ‰์…˜๋“ค์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.