ABOUT ME

  • java.lang.Object 클래스
    JAVA 2021. 7. 23. 09:51

    1. Object 클래스

     

    1) hashCode()

    객체의 해시코드

    객체의 해시코드란? 객체를 식별하는 하나의 정수값을 말한다. Object의 hashCode()메서드는

    객체의 메모리번지를 이용해서 정수값인 해시코드를 만들어 리턴하기 때문에 객체마다 다른

    값을 가지고 있다. 논리적으로 동등비교시 hashCode()를 재정의할 필요성이 있는 컬렉션프레임

    워크에서 HastSet, HashMap, HashTable은 equals()메서드를 이용해서 비교한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class Key {
        
        public int number;
        public Key(int number) {
            this.number = number;
        }
        
        @Override
        public int hashCode() {
            return number;
        }
        
        @Override
        public String toString() {
            return "toString: "+ number;
        }
    }
    cs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class KeyName {
     
        public static void main(String[] args) {
            Key key = new Key(1);
            System.out.println(key.toString());//패키지.클래스@16진수
            System.out.println(key.hashCode());//16진수 정수값
     
        }
    }
    cs

     

    2) toString()

    객체문자정보(toString())

    Object클래스의 toString()메서드는 객체의 문자정보를 리턴한다. 객체의 문자정보란

    객체를 문자열로 표현한 값을 말한다. 기본적으로 이 메서드는 "클래스명@16진수"의

    코드로 구성된 문자정보를 리턴한다.

    이 메서드의 리턴값은 실제적으로는 별 사용할 가치가 없는 정보이기 때문에 하위 클래스

    에서는 toString()메서드를 재정의해서 간결하고 유용한 정보를 리턴하도록 한다.

    예를 들어 재정의된 java.util.Date.toString()메서드를 보면 현재의 시스템날짜와 시간

    정보를 리턴한다. 그리고 String클래스는 toString()메서드를 재정의해서 객체가 가지고

    있는 문자열을 리턴한다.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class SmartPhone {
     
        private String company;
        private String os;
     
        public SmartPhone(String company, String os) {
            this.company = company;
            this.os = os;
        }
     
        @Override
        public String toString() {
            return company + "=" + os;
        }
     
    }
    cs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class SmartPhoneMain {
     
        public static void main(String[] args) {
     
            SmartPhone phone = new SmartPhone("애플","iOS");
            System.out.println(phone.toString());
            
            phone = new SmartPhone("LGE","안드로이드");
            System.out.println(phone.toString());
        }
    }
    cs

    3) clone()

    protected Object

    Cloneable이라는 인터페이스를 구현한 클래스만 사용 가능한 메서드.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class Member {
     
        public String id;
        public String name;
        public String password;
        public int age;
        public boolean adult;
        public int score[] = { 908292 };
        
        
        public Member(String id, String name, String password, int age, boolean adult) {
            this.id = id;
            this.name = name;
            this.password = password;
            this.age = age;
            this.adult = adult;
        }
    }
    cs
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
        public static void main(String[] args) {
     
            //1. 원본객체
            Member original = new Member("sohyang""소향""123456"42true);
            Member cloned = original;
            
            System.out.println(original.password);
            System.out.println(cloned.password);
            original.password = "abcdefg";
            System.out.println(original.password);
            System.out.println(cloned.password);
            System.out.println(original.password.hashCode());
            System.out.println(cloned.password.hashCode());
            System.out.println();
            
            original.age = 100;
            System.out.println(original.age);
            System.out.println(cloned.age);
        }
    cs

    댓글

Designed by Tistory.