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();
                }
                      
            }
      
        }
      

Categories:

Java