JAVA

14.[JAVA] 클래스 ToString ,equals 재정의

Gunadian 2022. 3. 7. 14:09

ToString()메서드

-Object  클래스의 메서드

-객체의 정보를 String으로 바꾸어서 사용할 때 많이 쓰임

-String 이나 Integet 클래스에서는 이미 재정의 되어 있음

-String은 문자열 반환

-Integer 는 정수 값 반환

 

package object;

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

public class ToStringEx {

	public static void main(String[] args) {
		
		Book book = new Book("자바스터디", "호호호");
		System.out.println(book);
		
		String str = new String("test"); //스트링 클래스에 to String이 정의되어있기 때문에 출력 된다.
		System.out.println(str);
				
	}
}

 

 

object.Book@7c30a502
test

출력시 콘솔창에는 위에와 같이 뜸

 

 

ToString() 메서드 재정의 하기

package s220307;

class Book{
	String title;
	String author;
	
	Book(String title, String author){
		this.title = title;
		this.author = author;
	}
	//재정의
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return title + "," + author ;
	}
}

public class ToStringEx {
	public static void main(String[] args) {
		
		Book book = new Book("자바스터디", "호호호");
		System.out.println(book);
		
		String str = new String("test"); //스트링 클래스에 to String이 정의되어있기 때문에 출력 된다.
		System.out.println(str);
				
	}
}

중간에 위에 와 같이 재정의를 한 후 출력하게 되면

자바스터디,호호호
test

위와 같이 출력 된다.


equals()메서드

-두 인스턴스의 주소 값을 비교하여 true/false를 반환

-재정의 하여 두 인스턴스가 논리적으로 동일함의 여부를 반환

-같은 학생인 경우 주소값은 다르지만,같은 학생으로 처리해야 학점이나 정보 산출에 문제가 생기지 않으므로 이런 경우 equals()메서드를 재정의 한다.

Student Studentkim = new Student(100,"김하하");
Student Studentkim2 = new Studentkim; //주소를 복사

 

package object;

class Student{
	int id;
	String name;
	
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	
	}

}

public class EqualsTest {

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

		System.out.println(str1 == str2); //주소가 같은지를 체크함 
		System.out.println(str1.equals(str2)); //str1과 str2의 문자열이 같은지를 체크함.
		
		Student std1 = new Student(1004, "하하");
		Student std2 = new Student(1004, "하하");
		
		System.out.println(std1 == std2);
		System.out.println(std1.equals(std2));
	}
}

출력시

false
true
false
false

위와 같이 출력 된다.

 

equals () 메서드 재정의 하기

package s220307;

class Student{
	int id;
	String name;
	
	public Student(int id, String name) {
		this.id = id;
		this.name = name;
	
	}

//재정의
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Student) {
			Student std = (Student)obj;
			if(id == std.id) 
				return true;
			else 
				return false;
		}
		return false;
	}
}

public class EqualsTest {

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

		System.out.println(str1 == str2); //주소가 같은지를 체크함 
		System.out.println(str1.equals(str2)); //str1과 str2의 문자열이 같은지를 체크함.
		
		Student std1 = new Student(1004, "하하");
		Student std2 = new Student(1004, "하하");
		
		System.out.println(std1 == std2);
		System.out.println(std1.equals(std2));
	}

}

출력시

false
true
false
true

'JAVA' 카테고리의 다른 글

13.[JAVA] 상속, 메소드 오버라이딩  (0) 2022.02.17
12.[JAVA] 접근제어  (0) 2021.12.06
11.[JAVA] 메소드 오버로딩  (0) 2021.12.06
10.[JAVA] 클래스 기초  (0) 2021.11.25
9.[JAVA] 객체 지향 프로그램 4가지 개념  (0) 2021.11.17