Java

자바 프로그래밍 (Java) - 34 : instanceof 연산자

n.han 2016. 7. 14. 13:12

- instanceof 연산자

 

instanceof는 캐스트 연산 가능성을 검사하는 연산자이다.

 

다음은 instanceof syntax와 이를 활용한 예제이다.

 

ps Student의 인스턴스가 아니므로 false return 된 것을 알 수 있다.

 

syntax : instanceName instanceof ClassName

public class MainClass {

        public static void main(String args[]) {

               Person ps = new Person();

               if(ps instanceof Student)

                       ps = (Student)ps;

               else

                       System.out.println("ps is not instance of the Student");

        }

}

출력 결과

ps is not instance of the Student