[JAVA]63.toString()

손영민's avatar
Mar 09, 2025
[JAVA]63.toString()
객체의 참조 변수를 호출할 때, 자동 호출되는 특이한 메서드이다.
자식이 오버라이드 해서 사용할 수 도 있다.
 

1.동일성 검증

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 == c2); System.out.println(c1.toString().equals(c2.toString())); } }
이미지
 
Share article

sson17