Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Goru

[Java]instanceof 연산자 본문

Java

[Java]instanceof 연산자

gorusair 2022. 1. 27. 22:52

참조변수가 참조하고 있는 인스턴스의 실제 타입을 알아보기 위해 instanceof 연산자를 사용한다.

주로 조건문에 사용되며, instanceof의 왼쪽에는 참조변수를 오른쪽에는 타입이 피연산자로 위치한다.

그리고 연산의 결과로 boolean값인 true와 false 중의 하나를 반환한다.

 

public class InstanceofTest {
	public static void main(String[] args) {
		FireEngine fe = new FireEngine();
		
		if(fe instanceof FireEngine) {
			System.out.println("This is a FireEngine instance");
		}
		
		if(fe instanceof Car) {
			System.out.println("This is a car instance.");
		}
		
		if(fe instanceof Object) {
			System.out.println("This is an Object instance");
		}
		
		System.out.println(fe.getClass().getName()); //클래스의 이름을 출력
	}
}
class Car{}
class FireEngine extends Car{}
This is a FireEngine instance
This is a car instance.
This is an Object instance
FireEngine

참조변수와 인스턴스의 연결

조상 클래스에 선언된 멤버변수와 같은 이름의 인스턴스변수를 자손 클래스에 중복으로 정의했을 떄, 조상타입의 참조변수로 자손 인스턴스를 참조하는 경우와 자손타입의 참조변수로 자손 인스턴스를 참조하는 경우는 서로 다른 결과를 얻는다.

멤버변수가 조상 클래스와 자손 클래스에 중복을 정의된 경우, 조상타입의 참조변수를 사용했을 떄는 조상 클래스에 선언된 멤버변수가 사용되고, 자손타입의 참조변수를 사용했을떄는 자손 클래스에 선언된 멤버변수가 사용된다.

하지만 중복 정의되지 않은 경우, 조상타입의 참조변수를 사용했을 떄와 자손타입의 참조변수를 사용했을 떄의 차이는 없다.

public class BindingTest {
	public static void main(String[] args) {
		Parent p = new Child();
		Child c = new Child();
		
		System.out.println("p.x = " +p.x);
		p.method();
		
		System.out.println("c.x= " +c.x);
		c.method();
	}
}
class Parent{
	int x = 100;
	
	void method() {
		System.out.println("Parent Method");
	}
}
class Child extends Parent{
	int x = 200;
	
	void method() {
		System.out.println("Child Method");
	}
}

p.x = 100
Child Method
c.x = 200
Child Method
class BindingTest2{
	public static void main(String[] args){
    	Parent p = new Child();
        Child c = new Child();
        
        System.out.println("p.x = " +p.x);
        p.method();
        
        System.out.pritnln("c.x = " +c.x);
        c.method();
        
    	}
   }
   
class Parent{
	int x = 100;
    
    void method(){
    	System.out.pritnln("Parent Method");
        }
     }
     
class Child extends Paret{  }

p.x =100
Parent Method
c.x =100
Parent Method

-> 이전 예제와 달리 Child클래스에는 아무런 멤버도 정의되어 있지 않고 단순히 조상으로부터 멤버들을 상속받는다. 그렇기 떄문에 참조변수의 타입에 관계없이 조상의 멤버들을 사용하게 된다.

 

class BindingTest3{
	public static void main(String[] args){
    	Parent p = new Child();
        Child c = new Child();
        
        System.out.println("p.x = " + p.x);
        p.method();
        System.out.println();
        System.out.println("c.x = " + c.x);
        c.method();
        }
     }
 class Parent{
 	int x = 100;
    
    void method(){
    	System.out.println("Parent Method");
        }
    }
 
 class Child extends Parent{
 	int x = 200;
    
    void method(){
    	System.out.println("x=" + x);	// this.x와 같다.
        System.out.println("super.x=" + super.x);
        System.out.println("this.x=" + this.x);
      }
  }
 p.x = 100
 x = 200
 super.x = 100
 this.x = 200
 
 c.x = 200
 x = 200
 super.x = 100
 this.x = 200

'Java' 카테고리의 다른 글

[JAVA]다형성  (0) 2022.01.26
[JAVA]제어자  (0) 2022.01.21
[Java]package 와 import  (0) 2022.01.20
[Java] 오버라이딩  (0) 2022.01.20
[Java] 클래스간의 관계- 포함관계  (0) 2022.01.19