[JAVA] 17. 제네릭 & Wrapper & 컬렉션 & 오브젝트 Ⅲ. Object 메서드 (3) toString()

최재원's avatar
Feb 18, 2025
[JAVA] 17. 제네릭 & Wrapper & 컬렉션 & 오브젝트 Ⅲ. Object 메서드 (3) toString()
객체의 참조 변수를 호출할 때, 자동 호출되는 특이한 메서드
자식이 오버라이드 해서, 사용할 수 도 있다.
notion image

동일성 검증

package ex17; class Card { private int no; private String name; private String content; public Card(int no, String name, String content) { this.no = no; this.name = name; this.content = content; } public int getNo() { return no; } public String getName() { return name; } public String getContent() { return content; } @Override public String toString() { return "Card{" + "no=" + no + ", name='" + name + '\'' + ", content='" + content + '\'' + '}'; } } public class Tos01 { public static void main(String[] args) { Card c1 = new Card(1, "마린", "소총 사격"); Card c2 = new Card(1, "마린", "소총 사격"); System.out.println(c1); System.out.println(c2); System.out.println(c1.toString().equals(c2.toString())); } }
notion image
  1. 객체에 toString() 메서드를 오버라이딩해서 문자열을 출력하게 만든 후
  1. equals()메서드를 사용해 비교하면
  1. 객체의 상태값을 비교할 수 있다
Share article

jjack1