객체의 타입 비교

yanju·2022년 11월 26일
0
post-thumbnail

객체가 특정 클래스인지 확인

public class TestClass{
}

TestClass tc = new TestClass();

// tc 객체는 TestClass 인가?
if(tc instanceof TestClass){
     
}

두 객체가 동일한 클래스인지 확인

TestClass tc1 = new TestClass();
TestClass tc2 = new TestClass();

 

// tc1과 tc2는 동일한 클래스인가? (클래스명은 몰라도 된다.)
if(tc1.getClass().isInstance(tc2)){

}

클래스 이름 리턴

String classname = tc.getClass().getName();

0개의 댓글

관련 채용 정보