본문 바로가기
study/Java

[Java] 22. instanceof 연산자

by 금이패런츠 2022. 3. 21.
728x90
반응형
package chap7;
/*
 * instanceof 연산자 : 객체와 참조변수의 관계를 알려주는 연산자
 * if (참조변수 instanceof 객체자료형)
 *   true : 참조변수가 참조하는 객체는 객체자료형으로 형변환이 가능함.
 *   false : 참조변수가 참조하는 객체는 객체자료형으로 형변환이 불가능함.
 */
public class InstanceOfEx1 {
	public static void main(String[] args) {
		Parent3 p = new Child3();
		if (p instanceof Child3) {
			System.out.println("p 참조변수 객체는 Child3 객체임");
		}
		if (p instanceof Parent3) {
			System.out.println("p 참조변수 객체는 Parent3 객체임");
		}
		p = new Parent3();
		if (p instanceof Child3) {
			System.out.println("p 참조변수 객체는 Child3 객체임");
		}
		if (p instanceof Parent3) {
			System.out.println("p 참조변수 객체는 Parent3 객체임");
		}
		if (p instanceof Object) {
			System.out.println("p 참조변수 객체는 Object 객체임");
		}
	}
}
728x90
반응형