๊ด€๋ฆฌ ๋ฉ”๋‰ด

โœ๐Ÿป๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ

[ JAVA ] ๊ธฐ๋ณธํด๋ž˜์Šค ( Object, String, Wrapper) ๋ณธ๋ฌธ

Language/Java

[ JAVA ] ๊ธฐ๋ณธํด๋ž˜์Šค ( Object, String, Wrapper)

์ฉ์‹œํ‚ด 2020. 12. 13. 21:26
728x90

Object ํด๋ž˜์Šค (java.lang.Object)

๋ชจ๋“  ํด๋ž˜์Šค์˜ ์ตœ์ƒ์œ„ ํด๋ž˜์Šค, ๋ชจ๋“  ํด๋ž˜์Šค๋Š” object ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์Œ

    -> ๋”ฐ๋กœ extends ํ•˜์ง€ ์•Š์•„๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ ๋ฐ”๋กœ toString () ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋Š” ์ด์œ 

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™ importํ•จ

์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ extendsํ•˜์ง€ ์•Š์•„๋„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋„ฃ์–ด์คŒ

java.langํŒจํ‚ค์ง€๋Š” ์ž๋ฐ”์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋™์ž‘์„ ํ•˜๋Š” ํด๋ž˜์Šค

java.langํŒจํ‚ค์ง€๋Š” ๋”ฐ๋กœ importํ•˜์ง€ ์•Š์•„๋„ ํด๋ž˜์Šค์— ์ด๋ฆ„๋งŒ์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ

ex) String, Void , System

 

Object ํด๋ž˜์Šค๋Š” ํ•„๋“œ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์œผ๋ฉฐ, ์ด 11๊ฐœ ๋ฉ”์„œ๋“œ๋กœ๋งŒ ๊ตฌ์„ฑ

1. toString()

์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜, ๋ฌธ์ž์—ด์€ ํด๋ž˜์Šค ์ด๋ฆ„@16์ง„์ˆ˜ ํ•ด์‰ฌ ์ฝ”๋“œ๋กœ ์ถœ๋ ฅ๋จ

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ์ถœ๋ ฅํ•จ

package object;

class Book{
    String title;
    String author;

    public Book(String title, String author){
        this.title = title;
        this.author  = author;
    }
   
}

public class ToStringTest {
    public static void main(String[] args) {    
        Book book = new Book("testBook", "testAuthor");
        System.out.println(book);
    }
}

 

toString() ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ apiํด๋ž˜์Šค๋งˆ๋‹ค ์ž์ฒด์ ์œผ๋กœ ์˜ค๋ฒ„ ๋ผ์ด๋”ฉ์„ ํ†ตํ•ด ์žฌ์ •์˜๋จ

@Override ์˜ค๋ธŒ์ ํŠธ ํด๋ž˜์Šค์˜ toString๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ๊ฐ์ฒด์˜ ๋‹ด๊ธด ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•จ

package object;

class Book{
    String title;
    String author;

    public Book(String title, String author){
        this.title = title;
        this.author  = author;
    }

   @Override
    public String toString() {
        return "Book{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}



public class ToStringTest {
    public static void main(String[] args) {
    
        Book book = new Book("testBook", "testAuthor");
        System.out.println(book);//toString์€ ๊ทธ ๊ฐ์ฒด์˜ ๊ธฐ๋ณธ์ •๋ณด๋ฅผ string ํ˜•ํƒœ๋กœ ์ฐ์–ด๋ƒ„

    }
}

๊ฐ์ฒด์˜ ์ •๋ณด๋ฅผ stringํ˜•ํƒœ๋กœ ๋ฐ”๊พธ์–ด ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉ

 

String str = new String("testBook"); 
System.out.println(str);

๋™์ผํ•˜๊ฒŒ new ์ƒ์„ฑ์ž๋กœ ์ถœ๋ ฅํ–ˆ์ง€๋งŒ ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋จ , String ํด๋ž˜์Šค ์•ˆ์— ์ด๋ฏธ toString ํด๋ž˜์Šค๊ฐ€ ์ •์˜๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ
์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๊ฒฝ์šฐ ์‹ค์ œ๋กœ ๋ถˆ๋ฆฌ๋Š” ๋ฉ”์„œ๋“œ๋Š” str.toString

 

2. equals()

๋‘ ๊ฐ์ฒด์˜ ๋™์ผํ•จ์„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ

  • ๋ฌผ๋ฆฌ์  ๋™์ผํ•จ : ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด
  • ๋…ผ๋ฆฌ์  ๋™์ผํ•จ : ๊ฐ™์€ ํ•™๋ฒˆ์˜ ํ•™์ƒ, ๊ฐ™์€ ์ฃผ๋ฌธ ๋ฒˆํ˜ธ์˜ ์ฃผ๋ฌธ
  • ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋‹ค๋ฅธ ๋ฉ”๋ชจ๋ฆฌ์— ์œ„์น˜ํ•œ ๊ฐ์ฒด๋ผ๋„ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋™์ผํ•จ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋งค์„œ๋“œ

 

2.1) String equals ์ฝ”๋“œ

str1 == str2๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ๋น„๊ตํ•˜๊ฒŒ ๋จ ๊ฐ ๊ฐ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ false ๋ฆฌํ„ด

String์˜ equals ๋ฉ”์„œ๋“œ๋Š” String์˜ ๋ฌธ์ž์—ด ๊ฐ’์ด ๊ฐ™์€์ง€ ๋น„๊ตํ•˜๋ฏ€๋กœ true ( object ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ ๋จ )

package object;

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

        System.out.println(str1);
        System.out.println(str2);
        
        System.out.println(str1 == str2); 
        System.out.println("str1.equals(str2) ์˜ ๊ฒฐ๊ณผ : "+str1.equals(str2)); 

    }
}

Object ํด๋ž˜์Šค์˜ equals๋ฅผ ์žฌ์ •์˜ํ•œ String ํด๋ž˜์Šค์˜ equals

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

2.2) ์žฌ์ •์˜๋œ ๊ฐ์ฒด์˜ equals

package object;

class Student{

    private int studentNum;
    private String studentName;

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

    @Override
    public boolean equals(Object obj) {
       if(obj instanceof Student){
           Student std = (Student)obj;
           return (this.studentNum == std.studentNum);
       }
       return false;
    }
}

public class EqualsTest {
    public static void main(String[] args) {
       
        Student Lee = new Student(100,"์ด์ˆœ์‹ ");
        Student Lee2 = Lee;
        System.out.println(Lee == Lee2);//true ๋™์ผํ•œ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฅดํ‚ด

        Student Shin = new Student(100,"์ด์ˆœ์‹ ");

        System.out.println(Lee == Shin);//false 
        System.out.println(Lee.equals(Shin));//Student ํด๋ž˜์Šค์— ์žฌ์ •์˜ํ•œ equals๋ฉ”์„œ๋“œ์— ์˜ํ•ด false ๊ฐ’ ๋ฆฌํ„ด

    }
}

3. hashCode()

4. clone()

5. filnalize()

6. getClass()

7. notify()

8. notifyAll()

9. wait() / wait(long timeout) / wait(long timeout, int nanos)

 

 


( ์ฐธ๊ณ  )

ํŒจ์ŠคํŠธ ์บ ํผ์Šค - Java ์›น ๊ฐœ๋ฐœ ๋งˆ์Šคํ„ฐ ์˜ฌ์ธ์› ํŒจํ‚ค์ง€ 

www.tcpschool.com/java/java_api_object

 

728x90
๋ฐ˜์‘ํ˜•