Object class / Methods of Object class
Object class
-
java.lang ν¨ν€μ§ (java.lang.Object)
-
νλ‘κ·Έλλ° μ μλμΌλ‘ import λ¨
-
λͺ¨λ ν΄λμ€μ μ΅μμ ν΄λμ€
-
λͺ¨λ ν΄λμ€λ Object ν΄λμ€ μμ, Object ν΄λμ€μ μΌλΆλ μ¬μ μ κ°λ₯
-
μ»΄νμΌλ¬κ° μλμΌλ‘ extends Object μΆκ°
-
Methods of Object class
toString() λ©μλ
-
ν΄λΉ μΈμ€ν΄μ€μ λν μ 보λ₯Ό λ¬Έμμ΄λ‘ λ°ν
-
ex)
-
Book.java
class Book{ private String title; private String author; public Book(String title, String author) { this.title = title; this.author = author; } // μ¬μ μ νμ§ μμΌλ©΄ μΈμ€ν΄μ€μ μ£Όμ μΆλ ₯ public String toString() { return title + "," + author; } }
-
BookMain.java
public class BookTest { public static void main(String[] args) { Book book = new Book("λ°λ―Έμ", "ν€λ₯΄λ§ ν€μΈ"); System.out.println(book); // = System.out.println(book.toString()); } }
-
equals() λ©μλ
-
λ μΈμ€ν΄μ€μ μ£Όμ κ°μ λΉκ΅ > true / false λ°ν
-
μ€λ²λΌμ΄λ© > λ μΈμ€ν΄μ€κ° λ Όλ¦¬μ μΌλ‘ λμΌνλ€λ©΄ true λ°ν
hashCode() λ©μλ
-
equals()λ©μλμ pair
-
μΈμ€ν΄μ€μ μ μ₯ μ£Όμ λ°ν
-
System.identityHashCode() λ©μλ : κ°μ²΄μ κ³ μ ν hashcode 리ν΄
-
-
ν΄μν¨μ : μμμ κΈΈμ΄λ₯Ό κ°λ μμμ λ°μ΄ν°μ λν΄ κ³ μ λ κΈΈμ΄μ λ°μ΄ν°λ‘ 맀ννλ ν¨μ
-
Hash table(ν΄μν μ΄λΈ) : Keyμ Valueλ₯Ό μ μ₯νλ λ°μ΄ν° ꡬ쑰
-
ν΄μ± : ν΄μν¨μλ₯Ό μ¬μ©νμ¬ μ£Όμ΄μ§ κ°μ λ³νν λ€, ν΄μ ν μ΄λΈμ μ μ₯νκ³ κ²μνλ κΈ°λ²
-
clone() λ©μλ
-
κ°μ²΄μ μλ³Έμ 볡μ
-
cloneable μΈν°νμ΄μ€ λͺ μ > ν΄λΉ ν΄λμ€μ clone() λ©μλμ μ¬μ©μ νμ©
-
κ°μ²΄μ μ λ³΄κ° λμΌν λ λ€λ₯Έ μΈμ€ν΄μ€ μμ± > κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μμμ μ 보 μλ, κ°μ²΄ 보νΈμ κ΄μ μμλ μλ°°λ¨
-
λ§μ½ κ°μ²΄μ λ³μκ° λ°λλ©΄ κ·Έ λ°λ κ°κΉμ§ μ λΆ λΆμ¬ (μμ± X, μΈμ€ν΄μ€μ μνλ₯Ό κ·Έλλ‘ λ³΅μ !)
-
μμΈμ²λ¦¬ ν΄μΌν¨
-
equals(), hashCode(), clone() ex)
-
Student.java
public class Student implements Cloneable{ private int studentNum; private String studentName; public Student(int studentNum, String studentName) { this.studentNum = studentNum; this.studentName = studentName; } public String toString() { return studentNum + ", " + studentName; } //μ€λ²λΌμ΄λ© > 물리μ (μ£Όμκ°)μ΄ μλ λ Όλ¦¬μ μΌλ‘ κ°μμ§ λ°ν @Override public boolean equals(Object obj) { if (obj instanceof Student) { Student std = (Student)obj; //λ€μ΄μΊμ€ν if (this.studentNum == std.studentNum) { return true; } else return false; } return false; } @Override public int hashCode() { return studentNum; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
-
Test.java
public class Test { public static void main(String[] args) { Student std1 = new Student(1234, "μ΄μλ°"); Student std2 = new Student(1234, "μ€νμ΄"); Student std3 = std1; System.out.println(std1 == std2); //λ€λ₯Έ κ°μ²΄ λ€λ₯Έ μ£Όμκ° > false μΆλ ₯ System.out.println(std1.equals(std2)); //μ€λ²λΌμ΄λ© νκΈ° μ : λ€λ₯Έ κ°μ²΄ λ€λ₯Έ μ£Όμκ° > false μΆλ ₯ //μ€λ²λΌμ΄λ© ν : true μΆλ ₯ System.out.println(std1 == std3); //κ°μ μ£Όμκ° > true μΆλ ₯ System.out.println(std1.hashCode()); System.out.println(std2.hashCode()); //μ€λ²λΌμ΄λ© μ : μλ‘ λ€λ₯Έ κ° μΆλ ₯ //μ€λ²λΌμ΄λ© ν : κ°μ κ° μΆλ ₯ try { Student copyStudent = (Student)std1.clone(); System.out.println(copyStudent); //1234, μ΄μλ° μΆλ ₯ } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }
-