โถ 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์ด ๋ฐ์ํ๋ค.
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java-๊ธฐ์ด] ๊ทธ ๋ฐ์ ๋ค๋ฅธ String ๊ด๋ จ ๋ฉ์๋ (0) | 2021.02.21 |
---|---|
[Java-๊ธฐ์ด] String ํด๋์ค (0) | 2021.02.21 |
[Java-๊ธฐ์ด] ์ธํฐํ์ด์ค ์์ (0) | 2021.02.21 |
[Java-๊ธฐ์ด] ์ธํฐํ์ด์ค ํ์ฉํ๊ธฐ (0) | 2021.02.21 |
[Java] ์ ์ ๋ฉ์๋ (0) | 2021.02.21 |
๋๊ธ