๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] Object ํด๋ž˜์Šค

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 21.
๋ฐ˜์‘ํ˜•

โ–ถ Object ํด๋ž˜์Šค

๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค. ๋‹ค์‹œ ๋งํ•˜๋ฉด ๋ชจ๋“  ํด๋ž˜์Šค๋Š” Object ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ์ƒ์†์„ ๋ฐ›๋Š”๋‹ค.

๊ตณ์ด extends Object ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜ํ•จ.

โ€‹

โ€‹

 

*toString( ) ๋ฉ”์„œ๋“œ

package object;

class Book {
	int bookNumber;
	String bookTitle;

	Book(int bookNumber, String bookTitle) {  // ์ฑ… ๋ฒˆํ˜ธ์™€ ์ œ๋ชฉ์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ์ƒ์„ฑ์ž
		this.bookNumber = bookNumber;
		this.bookTitle = bookTitle;
	}
}

public class ToStringEx {
	public static void main(String[] args) {
		Book book1 = new Book(200, "๊ฐœ๋ฏธ");

		System.out.println(book1);  // ์ธ์Šคํ„ด์Šค ์ •๋ณด(ํด๋ž˜์Šค ์ด๋ฆ„, ์ฃผ์†Œ ๊ฐ’)
		System.out.println(book1.toString());  // toString() ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค ์ •๋ณด (ํด๋ž˜์Šค ์ด๋ฆ„, ์ฃผ์†Œ ๊ฐ’)๋ฅผ ๋ณด์—ฌ์คŒ
	}
}

-์—ฌ๊ธฐ์„œ ํ˜ธ์ถœ๋˜๋Š” toString( ) ์€ Book ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์•„๋‹Œ Object ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ์ด๋‹ค.

โ€‹

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹โ€‹

โ€‹

โ€‹

*String๊ณผ Integer ํด๋ž˜์Šค์˜ toString( ) ๋ฉ”์„œ๋“œ

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

โ€‹

โ€‹

โ€‹โ€‹

*Book ํด๋ž˜์Šค์—์„œ toString( ) ๋ฉ”์„œ๋“œ ์ง์ ‘ ์žฌ์ •์˜ํ•˜๊ธฐ

package object;

class Book {
	int bookNumber;
	String bookTitle;

	Book(int bookNumber, String bookTitle) {
		this.bookNumber = bookNumber;
		this.bookTitle = bookTitle;
	}

	@Override
	public String toString() {
		return bookTitle + "," + bookNumber;
	}
}

public class ToStringEx {
	public static void main(String[] args) {
		Book book1 = new Book(200, "๊ฐœ๋ฏธ");

		System.out.println(book1);
		System.out.println(book1.toString());
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

*equal( ) ๋ฉ”์„œ๋“œ

equal( ) ๋ฉ”์„œ๋“œ์˜ ์›๋ž˜ ๊ธฐ๋Šฅ์€ ๋‘ ์ธ์Šคํ„ด์Šค์˜ ์ฃผ์†Œ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ boolean ๊ฐ’(T/F)์„ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

์ฃผ์†Œ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด ์ธ์Šคํ„ด์Šค์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์„œ๋กœ ๋‹ค๋ฅธ ์ฃผ์†Œ ๊ฐ’์„ ๊ฐ€์งˆ ๋•Œ๋„ ๊ฐ™์€ ์ธ์Šคํ„ด์Šค๋ผ๊ณ  ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฌผ๋ฆฌ์  ๋™์ผ์„ฑ(์ธ์Šคํ„ด์Šค์˜ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ๊ฐ™์Œ)๋ฟ ์•„๋‹ˆ๋ผ ๋…ผ๋ฆฌ์  ๋™์ผ์„ฑ(๋…ผ๋ฆฌ์ ์œผ๋กœ ๋‘ ์ธ์Šคํ„ด์Šค๊ฐ€ ๊ฐ™์Œ)์„ ๊ตฌํ˜„ํ•  ๋•Œ๋„ equal( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

package object;

class Student {
	int studentID;
	String studentName;

	public Student(int studentID, String studentName) {
		this.studentID = studentID;
		this.studentName = studentName;
	}

	public String toString() {
		return studentID + "," + studentName;
	}

public class EqualsTest {
	public static void main(String[] args) {
		Student studentLee = new Student(100, "์ด์ƒ์›");
		Student studentLee2 = studentLee;  // ์ฃผ์†Œ ๋ณต์‚ฌ
		Student studentSang = new Student(100, "์ด์ƒ์›");

		if (studentLee == studentLee2)  // == ๊ธฐํ˜ธ๋กœ ๋น„๊ต
			System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.");
		else
			System.out.println("studentLee์™€ studentLee2์˜ ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค.");

		if (studentLee.equals(studentLee2))  // equals() ๋ฉ”์„œ๋“œ๋กœ ๋น„๊ต
			System.out.println("studentLee์™€ studentLee2๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.");
		else
			System.out.println("studentLee์™€ studentLee2๋Š” ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");

		if (studentLee == studentSang)  // == ๊ธฐํ˜ธ๋กœ ๋น„๊ต
			System.out.println("studentLee์™€ studentSang์˜ ์ฃผ์†Œ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.");

		else
			System.out.println("studentLee์™€ studentSang์˜ ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ…๋‹ˆ๋‹ค.");

		if (studentLee.equals(studentSang))  // equals() ๋ฉ”์„œ๋“œ๋กœ ๋น„๊ต
			System.out.println("studentLee์™€ studentSang์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.");
		else
			System.out.println("studentLee์™€ studentSang์€ ๋™์ผํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

 

*String๊ณผ Integer ํด๋ž˜์Šค์˜ equals( ) ๋ฉ”์„œ๋“œ

package object;

public class StringEquals {
	public static void main(String[] args) {
		String str1 = new String("abc");
		String str2 = new String("abc");

		System.out.println(str1 == str2);  // ๋‘ ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜์—ฌ ์ถœ๋ ฅ
		System.out.println(str1.equals(str2));  // String ํด๋ž˜์Šค์˜ equals() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ. ๋‘ ์ธ์Šคํ„ด์Šค์˜ ๋ฌธ์ž์—ด ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜์—ฌ ์ถœ๋ ฅ

		Integer i1 = new Integer(100);
		Integer i2 = new Integer(100);

		System.out.println(i1 == i2);  // ๋‘ ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜์—ฌ ์ถœ๋ ฅ
		System.out.println(i1.equals(i2));  // Integer ํด๋ž˜์Šค์˜ equals() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ. ๋‘ ์ธ์Šคํ„ด์Šค์˜ ๋ฌธ์ž์—ด ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜์—ฌ ์ถœ๋ ฅ
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

*Student ํด๋ž˜์Šค์—์„œ equals( ) ๋ฉ”์„œ๋“œ ์ง์ ‘ ์žฌ์ •์˜ํ•˜๊ธฐ

package object;

class Student {
	int studentID;
	String studentName;

	public Student(int studentID, String studentName) {
		this.studentID = studentID;
		this.studentName = studentName;
	}

	public String toString() {
		return studentID + "," + studentName;
	}

	@Override
	public boolean equals(Object obj) {  // equals() ๋ฉ”์„œ๋“œ ์žฌ์ •์˜
		if (obj instanceof Student) {
			Student std = (Student) obj;
			if (this.studentID == std.studentID)  // ์žฌ์ •์˜ํ•œ equals() ๋ฉ”์„œ๋“œ๋Š” ํ•™์ƒ์˜ ํ•™๋ฒˆ์ด ๊ฐ™์œผ๋ฉด true ๋ฐ˜ํ™˜
				return true;
			else
				return false;
		}
		return false;
	}
    
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

-equals( ) ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” Objectํ˜•์ด๋‹ค. ๋น„๊ต๋  ๊ฐ์ฒด๊ฐ€ Objectํ˜• ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋ฉด instanceof๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์›๋ž˜ ์ž๋ฃŒํ˜•์ด Student์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 10~11ํ–‰์—์„œ this์˜ ํ•™๋ฒˆ๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋œ ๊ฐ์ฒด์˜ ํ•™๋ฒˆ์ด ๊ฐ™์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•œ ํ›„ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด studentLee์™€ studentSang์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ์— ์กด์žฌํ•˜๋Š” ์ธ์Šคํ„ด์Šค์ด๋ฏ€๋กœ == ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ ๊ฐ™์€ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, ํ•™๋ฒˆ์ด ๊ฐ™์œผ๋ฏ€๋กœ equals( ) ๋Š” true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

โ€‹

 

 

๋‚˜ํ˜ผ์ž ์ฝ”๋”ฉ) MyDate ํด๋ž˜์Šค์˜ equals( ) ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ

๋‚ ์งœ๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค MyDate๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋‚ ์งœ๊ฐ€ ๊ฐ™์œผ๋ฉด System.out.println(date1. equals(date2));์˜ ์ถœ๋ ฅ ๊ฒฐ๊ณผ ๊ฐ’์ด true๊ฐ€ ๋˜๋„๋ก equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๋ผ.

โ€‹

-'์›”'๋งŒ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ

package object;

class MyDate {
	int day;
	int month;
	int year;

	public MyDate(int month, int day, int year) {
		this.day = day;
		this.month = month;
		this.year = year;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MyDate) {
			MyDate date = (MyDate) obj;
			if (this.day == date.day && this.month == date.month && this.year == date.year)
				return true;
			else
				return false;
		}
		return false;
	}
}

public class MyDateTest {
	public static void main(String[] args) {
		MyDate date1 = new MyDate(8, 18, 2004);
		MyDate date2 = new MyDate(9, 18, 2004);
		System.out.println(date1.equals(date2));
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

-'์ผ'๋งŒ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ

public class MyDateTest {
	public static void main(String[] args) {
		MyDate date1 = new MyDate(9, 15, 2004);
		MyDate date2 = new MyDate(9, 18, 2004);
		System.out.println(date1.equals(date2));
    }
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

-๋…„๋„ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ

public class MyDateTest {
	public static void main(String[] args) {
		MyDate date1 = new MyDate(9, 18, 2020);
		MyDate date2 = new MyDate(9, 18, 2004);
		System.out.println(date1.equals(date2));
    }
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

-์ถœ๋ ฅ์ด ์ž˜ ๋œ๋‹ค !

โ€‹

 

 

*hashcode( ) ๋ฉ”์„œ๋“œ

ํ•ด์‹œ(hash)๋Š” ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ.

์ •๋ณด๋ฅผ ์–ด๋””์— ์ €์žฅํ•  ๊ฒƒ์ธ์ง€, ์–ด๋””์„œ ๊ฐ€์ ธ์˜ฌ ๊ฒƒ์ธ์ง€ ํ•ด์‹œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•œ๋‹ค.

ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์˜ ํŠน์ • ์ •๋ณด(ํ‚ค ๊ฐ’)๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์œผ๋กœ ๋„ฃ์œผ๋ฉด ๊ทธ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋˜์–ด์•ผ ํ•  ์œ„์น˜๋‚˜ ์ €์žฅ๋œ ํ•ด์‹œ ํ…Œ์ด๋ธ” ์ฃผ์†Œ(์œ„์น˜)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด ์ •๋ณด๋ฅผ ์•Œ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด์˜ ์œ„์น˜๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•ด์‹œ ํ•จ์ˆ˜๋Š” ๊ฐœ๋ฐœํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„๋œ๋‹ค.

equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ–ˆ๋‹ค๋ฉด hashCode( ) ๋ฉ”์„œ๋“œ๋„ ์žฌ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

โ€‹

โ€‹โ€‹

โ€‹

*String๊ณผ Integer ํด๋ž˜์Šค์˜ hashCode( ) ๋ฉ”์„œ๋“œ

String ํด๋ž˜์Šค์™€ Integer ํด๋ž˜์Šค์˜ equals( ) ๋ฉ”์„œ๋“œ๋Š” ์žฌ์ •์˜๋˜์–ด ์žˆ๋‹ค๊ณ  ํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด hashCode( ) ๋ฉ”์„œ๋“œ๋„ ํ•จ๊ป˜ ์žฌ์ •์˜๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

package object;

public class HashCodeTest {
	public static void main(String[] args) {
		String str1 = new String("abc");
		String str2 = new String("abc");

		System.out.println(str1.hashCode());
		System.out.println(str2.hashCode());  // abc ๋ฌธ์ž์—ด์˜ ํ•ด์‹œ์ฝ”๋“œ๊ฐ’ ์ถœ๋ ฅ

		Integer i1 = new Integer(100);
		Integer i2 = new Integer(100);

		System.out.println(i1.hashCode());
		System.out.println(i2.hashCode());  // Integer(100)์˜ ํ•ด์‹œ์ฝ”๋“œ๊ฐ’ ์ถœ๋ ฅ
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹โ€‹

โ€‹

*Student ํด๋ž˜์Šค์—์„œ hashCode( ) ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ

package object;

class Student {
	int studentID;
	String studentName;

    ...

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

public class EqualsTest {
	public static void main(String[] args) {
		...

		System.out.println("studentLee์˜ hashCode : " + studentLee.hashCode());
		System.out.println("studentSang์˜ hashCode : " + studentSang.hashCode());

		System.out.println("studentLee์˜ ์‹ค์ œ ์ฃผ์†Œ ๊ฐ’ : " + System.identityHashCode(studentLee));
		System.out.println("studentSang์˜ ์‹ค์ œ ์ฃผ์†Œ ๊ฐ’ : " + System.identityHashCode(studentSang));
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

 

 

๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ) MyDate ํด๋ž˜์Šค์˜ hashCode( ) ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ํ•˜๊ธฐ

362์ชฝ์˜ <๋‚˜ ํ˜ผ์ž ์ฝ”๋”ฉ!> ์ฝ”๋„ˆ์—์„œ ๋งŒ๋“  MyDate ํด๋ž˜์Šค์— equals( )๋ฅผ ์žฌ์ •์˜ํ–ˆ์„๊ฑฐ๋‹ค.

equals( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ hashCode( ) ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด๋ณด๋ผ.

    @Override
	public int hashCode() {
		return Integer.parseInt(year + "" + month + "" + day);
		// return Integer.parseInt(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));
	}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

 

 

*clone( ) ๋ฉ”์„œ๋“œ

๊ฐ์ฒด ์›๋ณธ์„ ์œ ์ง€ํ•ด ๋†“๊ณ  ๋ณต์‚ฌ๋ณธ์„ ์‚ฌ์šฉํ•œ๋‹ค๊ฑฐ๋‚˜, ๊ธฐ๋ณธ ํ‹€์˜ ๋ณต์‚ฌ๋ณธ์„ ์‚ฌ์šฉํ•ด ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณต์žกํ•œ ์ƒ์„ฑ ๊ณผ์ •์„ ๊ฐ„๋‹จํžˆ ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— clone( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. clone( ) ๋ฉ”์„œ๋“œ๋Š” Object์— ์˜ค๋ฅธ์ชฝ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•ด ๋˜ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

package object;

class Point {  // ์›์ ์„ ์˜๋ฏธํ•˜๋Š” Point ํด๋ž˜์Šค
	int x;
	int y;

	Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public String toString() {
		return "x = " + x + ", " + "y = " + y;
	}
}

class Circle implements Cloneable {  // ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•ด๋„ ๋œ๋‹ค๋Š” ์˜๋ฏธ๋กœ Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•จ๊ป˜ ์„ ์–ธ
	Point point;
	int radius;

	Circle(int x, int y, int radius) {
		this.radius = radius;
		point = new Point(x, y);
	}

	public String toString() {
		return "์›์ ์€ " + point + "์ด๊ณ , " + "๋ฐ˜์ง€๋ฆ„์€ " + radius + "์ž…๋‹ˆ๋‹ค.";
	}

	@Override
	public Object clone() throws CloneNotSupportedException {  // clone() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜๋ฅผ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•จ
		return super.clone();
	}
}

public class ObjectCloneTest {
	public static void main(String[] args) throws CloneNotSupportedException {
		Circle circle = new Circle(10, 20, 30);
		Circle copyCircle = (Circle) circle.clone();  // clone() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด circle ์ธ์Šคํ„ด์Šค๋ฅผ copyCircle์— ๋ณต์ œํ•จ

		System.out.println(circle);
		System.out.println(copyCircle);
		System.out.println(System.identityHashCode(circle));
		System.out.println(System.identityHashCode(copyCircle));
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

clone( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋ณต์ œํ•ด๋„ ๋œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ํด๋ž˜์Šค์— Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

๋งŒ์•ฝ clone( ) ๋ฉ”์„œ๋“œ๋งŒ ์žฌ์ •์˜ํ•˜๊ณ  Cloneable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์œผ๋ฉด clone( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ CloneNotSupportedException์ด ๋ฐœ์ƒํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€