๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

์ž๋ฐ” Object ํด๋ž˜์Šค ์ •๋ฆฌ - toString(), equals(), hashCode(), clone()

by Leica 2020. 6. 30.
๋ฐ˜์‘ํ˜•

์ž๋ฐ” Object ํด๋ž˜์Šค ์ •๋ฆฌ - toString(), equals(), hashCode(), clone()

Object ํด๋ž˜์Šค

- java.lang.Object
- ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค

Object ํด๋ž˜์Šค๋Š” ์ž๋ฐ”์—์„œ ์ œ๊ณตํ•˜๋Š” ์ค‘์š”ํ•œ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

java.lang.Object ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค๋กœ ๋ชจ๋“  ํด๋ž˜์Šค๋Š” Object ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ชจ๋“  ํด๋ž˜์Šค๋Š” Object ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์ผ๋ถ€ ๋ฉ”์†Œ๋“œ๋ฅผ overrideํ•ด์„œ ์‚ฌ์šฉํ• ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ, ๋‹น์—ฐํžˆ final ๋ฉ”์†Œ๋“œ๋Š” overrideํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

Object ํด๋ž˜์Šค๊ฐ€ ๋“ค์–ด์žˆ๋Š” java.lang ํŒจํ‚ค์ง€๋Š” ์ปดํŒŒ์ผ๋Ÿฌ์— ์˜ํ•ด ์ž๋™์œผ๋กœ import๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ java.lang ํŒจํ‚ค์ง€์— ์žˆ๋Š” ํด๋ž˜์Šค๋Š” ๋”ฐ๋กœ importํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฃผ๋กœ clone(), equals(), hashCode(), toString()์„ ํ”ํžˆ overrideํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค.

 

1. toString()

- ๊ธฐ๋ณธ ๋™์ž‘: ๊ฐ์ฒด์˜ ํ•ด์‹œ์ฝ”๋“œ ์ถœ๋ ฅ
- override ๋ชฉ์ : ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•  ๋•Œ

 

package com.yoon._object;

public class Book {

    private String title;
    private String author;

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

    public static void main(String[] args) {

        Book book = new Book("๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ", "๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ");
        System.out.println(book);
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

com.yoon._object.Book@35bbe5e8

Process finished with exit code 0

 

์ด๋ ‡๊ฒŒ toString()์„ overrideํ•˜์ง€ ์•Š๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด 'ํด๋ž˜์Šคํ’€๋„ค์ž„@ํ•ด์‹œ์ฝ”๋“œ'๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด toString()์˜ ์›ํ˜•์ž…๋‹ˆ๋‹ค.

 

toString()์˜ ์›ํ˜•

getClass().getName() + '@' + Integer.toHexString(hashCode())

 

๋‹ค๋ฅธ ์˜ˆ์ œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

String str = new String("Apple");
System.out.println(str);

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

Apple

Process finished with exit code 0

 

String ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด String ๊ฐ์ฒด๊ฐ€ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

์ด ์ด์œ ๋Š” jdk์˜ String ํด๋ž˜์Šค๋Š” toString()์„ overrideํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

System.out.println(str)์€ System.out.println(str.toString())๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ฆ‰ toString()์€ ์–ด๋–ค ๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•ด์•ผ ํ•  ๋•Œ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

 

๋‹ค์‹œ Book ์˜ˆ์ œ์—์„œ toString()์„ overrideํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.

 

package com.yoon._object;

public class Book {

    private String title;
    private String author;

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

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    public static void main(String[] args) {

        Book book = new Book("๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ", "๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ");
        System.out.println(book);
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

Book{title='๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ', author='๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ'}

Process finished with exit code 0

 

2. equals()

- ๊ธฐ๋ณธ ๋™์ž‘: '==' ์—ฐ์‚ฐ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
- override ๋ชฉ์ : ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ์— ์œ„์น˜ํ•˜๋Š” ๊ฐ์ฒด์—ฌ๋„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•จ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด

equals()๋ฅผ ์‚ฌ์šฉํ•ด ๋‘ ๊ฐ์ฒด์˜ ๋™์ผํ•จ์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ override ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

'๋ฌผ๋ฆฌ์  ๋™์ผํ•จ'์€ ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์„, '๋…ผ๋ฆฌ์  ๋™์ผํ•จ'์€ ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋‹ค๋ฅธ ์œ„์น˜์— ์žˆ์ง€๋งŒ ๊ฐ™์€ id์˜ ํšŒ์› ๊ฐ์ฒด, ๊ฐ™์€ id์˜ ์ฃผ๋ฌธ ๊ฐ์ฒด์™€ ๊ฐ™์ด ๋„๋ฉ”์ธ์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์œ ํ•œ ๊ฐ’ ๋“ฑ์— ์˜ํ•ด ๋™์ผํ•œ ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

public class User {

    int id;
    String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public static void main(String[] args) {

        User user1 = new User(1001, "ํ™๊ธธ๋™");
        User user2 = new User(1001, "ํ™๊ธธ๋™");
        
        System.out.println(user1.equals(user2));
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

false

Process finished with exit code 0

 

equals()์˜ ๊ธฐ๋ณธ ๋™์ž‘์€ '==' ์—ฐ์‚ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ equals()๋กœ ๋น„๊ตํ•˜๋ฉด false๊ฐ€ ๋ฆฌํ„ด๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ equals()๋ฅผ overrideํ•˜์—ฌ ๋…ผ๋ฆฌ์ ์ธ ๋™์ผ์„ฑ์„ ๊ฐ–๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์œ„ ์˜ˆ์ œ์—์„œ User ํด๋ž˜์Šค์—์„œ equals()๋ฅผ override  ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

public class User {

    int id;
    String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof User) {
            return this.getId() == ((User)obj).getId();
        } else {
            return false;
        }
    }

    public static void main(String[] args) {

        User user1 = new User(1001, "ํ™๊ธธ๋™");
        User user2 = new User(1001, "ํ™๊ธธ๋™");

        System.out.println(user1.equals(user2));
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

true

Process finished with exit code 0

 

equals()์˜ ๋ฐ˜ํ™˜๊ฐ’์ด true๋กœ ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ๋Š” ๊ฐ™์€ ํ•ด์‹œ์ฝ”๋“œ๊ฐ’์„ ๊ฐ–๋Š”๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

hashCode()

- ๊ธฐ๋ณธ ๋™์ž‘: JVM์ด ๋ถ€์—ฌํ•œ ์ฝ”๋“œ๊ฐ’. ์ธ์Šคํ„ด์Šค๊ฐ€ ์ €์žฅ๋œ ๊ฐ€์ƒ๋จธ์‹ ์˜ ์ฃผ์†Œ๋ฅผ 10์ง„์ˆ˜๋กœ ๋ฐ˜ํ™˜
- override ๋ชฉ์ : ๋‘ ๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ์— ์œ„์น˜ํ•œ ๊ฐ์ฒด๊ฐ€ ๋™์ผ์„ฑ์„ ๊ฐ–๊ธฐ ์œ„ํ•ด

 

ํ•ด์‹œ์ฝ”๋“œ๋ž€ JVM์ด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๋ณ€ํ™˜ํ•ด์„œ ๋ถ€์—ฌํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ’๊ณผ๋Š” ๋ณ„๊ฐœ์˜ ๊ฐ’์ด๋ฉฐ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋Š” System ํด๋ž˜์Šค์˜ identityHashCode()๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ์˜ ๋™์ผ์„ฑ์€ equals()์˜ ๋ฐ˜ํ™˜๊ฐ’์ด true, hashCode() ๋ฐ˜ํ™˜๊ฐ’์ด ๋™์ผํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ผ๋ฐ˜์ ์œผ๋กœ equals()์™€ hashCode()๋Š” ํ•จ๊ป˜ override ํ•ฉ๋‹ˆ๋‹ค.

 

public class User {

    int id;
    String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof User) {
            return this.getId() == ((User)obj).getId();
        } else {
            return false;
        }
    }

    public static void main(String[] args) {

        User user1 = new User(1001, "ํ™๊ธธ๋™");
        User user2 = new User(1001, "ํ™๊ธธ๋™");

        System.out.println("user1.equals(user2): " + user1.equals(user2));
        System.out.println("user1.hashCode(): " + user1.hashCode());
        System.out.println("user2.hashCode(): " + user2.hashCode());
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

user1.equals(user2): true
user1.hashCode(): 901506536
user2.hashCode(): 747464370

Process finished with exit code 0

 

์ง€๊ธˆ์€ User ๊ฐ์ฒด user1๊ณผ user2๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ž๋ฐ”์—์„œ์˜ ๋™์ผ์„ฑ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” hashCode()๋„ override ํ•ด์•ผํ•˜๋ฉฐ, equals()๋ฅผ overrideํ•  ๋•Œ ์‚ฌ์šฉํ•œ ๋ฉค๋ฒ„๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ๋Š” id๊ฐ€ ๋˜๊ฒ ์ง€์š”.

 

public class User {

    int id;
    String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof User) {
            return this.getId() == ((User)obj).getId();
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return getId();
    }

    public static void main(String[] args) {

        User user1 = new User(1001, "ํ™๊ธธ๋™");
        User user2 = new User(1001, "ํ™๊ธธ๋™");

        System.out.println("user1.equals(user2): " + user1.equals(user2));
        System.out.println("user1.hashCode(): " + user1.hashCode());
        System.out.println("user2.hashCode(): " + user2.hashCode());
        System.out.println("System.identityHashCode(user1): " + System.identityHashCode(user1));
        System.out.println("System.identityHashCode(user2): " + System.identityHashCode(user2));
    }
}

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

user1.equals(user2): true
user1.hashCode(): 1001
user2.hashCode(): 1001
System.identityHashCode(user1): 901506536
System.identityHashCode(user2): 747464370

Process finished with exit code 0

 

์ž๋ฐ”์˜ Integer ํด๋ž˜์Šค๋„ Object ํด๋ž˜์Šค์˜ hashCode()๋ฅผ overrideํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

Integer i1 = 100;
Integer i2 = 100;

System.out.println("i1.hashCode(): " + i1.hashCode());
System.out.println("i2.hashCode(): " + i2.hashCode());

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

i1.hashCode(): 100
i2.hashCode(): 100

Process finished with exit code 0

 

clone()

- ๊ฐ์ฒด์˜ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ฆ
- Prototype์œผ๋กœ๋ถ€ํ„ฐ ๊ฐ™์€ ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง„ ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑ
- ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์˜ ์ •๋ณด์€๋‹‰์— ์œ„๋ฐฐ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์œผ๋ฏ€๋กœ ๋ณต์ œํ•  ๊ฐ์ฒด๋Š” Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ช…์‹œํ•ด์•ผ ํ•จ

 

clone() ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ private ํ•„๋“œ๋„ ๋ณต์ œํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ณด์€๋‹‰์— ์œ„๋ฐฐ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ Cloneable ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ช…์‹œ๋ผ์žˆ๋Š” ํด๋ž˜์Šค๋งŒ clone()์„ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์œ„์—์„œ ์‚ฌ์šฉํ•œ Book ํด๋ž˜์Šค์—์„œ clone()์„ overrideํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public class Book {

    private String title;
    private String author;

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

    @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {

        Book book1 = new Book("๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ", "๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ");
        System.out.println(book1);
        
        Book book2 = (Book) book1.clone();
        System.out.println(book2);
    }
}

 

clone()์€ super.clone()์„ ๊ทธ๋Œ€๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

Book{title='๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ', author='๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ'}
Exception in thread "main" java.lang.CloneNotSupportedException: com.yoon._object.Book
	at java.base/java.lang.Object.clone(Native Method)
	at com.yoon._object.Book.clone(Book.java:23)
	at com.yoon._object.Book.main(Book.java:31)

Process finished with exit code 1

 

CloneNotSupportedException์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์ด์œ ๋Š” Book ํด๋ž˜์Šค์— Cloneable ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋ช…์‹œ๋ผ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋‹ค์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

 

public class Book implements Cloneable

 

๐Ÿ–ฅ ์‹คํ–‰ ๊ฒฐ๊ณผ

Book{title='๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ', author='๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ'}
Book{title='๊นŠ์€ ๋ฐค ๋ถ€์—Œ์—์„œ', author='๋ชจ๋ฆฌ์Šค ์ƒŒ๋‹ฅ'}

Process finished with exit code 0

 

Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋นˆ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

public interface Cloneable {
}

 

๋”ฐ๋ผ์„œ Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implementsํ•ด๋„ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋ฉ”์†Œ๋“œ๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ '๋งˆํฌ ์ธํ„ฐํŽ˜์ด์Šค'๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

์ •๋ฆฌํ•˜๋ฉด clone()์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

- Cloneable ์ธํ„ฐํŽ˜์ด์Šค ๋ช…์‹œ ํ•„์š”

- ๋ณต์ œ๋œ ๊ฐ์ฒด๋Š” Object๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ ๋ช…์‹œ์ ์ธ ์บ์ŠคํŒ… ํ•„์š”

- ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ณต์ œ๋˜๋ฉด์„œ CloneNotSupportedException์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•„์š”

 

์ฐธ๊ณ  - finalize()

Object ํด๋ž˜์Šค์˜ finalize() ๋ฉ”์†Œ๋“œ๋Š” ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด๊ฐ€ ํž™ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋  ๋•Œ ๊ฐ€๋น„์ง€์ฝœ๋ ‰ํ„ฐ๊ฐ€ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.

์ด ๋ฉ”์†Œ๋“œ๊ฐ€ override๋ผ์žˆ์œผ๋ฉด ๊ฐ€๋น„์ง€์ฝœ๋ ‰ํ„ฐ๊ฐ€ ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰ finalize()์—๋Š” ๊ฐ์ฒด๊ฐ€ ํ•ด์ œ๋  ๋•Œ ๋ฆฌ์†Œ์Šค ํ•ด์ œ, ์†Œ์ผ“ close ๋“ฑ์˜ ํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€