강의

멘토링

커뮤니티

Inflearn Community Q&A

njhzi5114's profile image
njhzi5114

asked

Kim Young-han's Practical Java - Intermediate Level 1

래퍼 클래스 - 참조형 기본형 비교

Resolved

Written on

·

165

0

public class WrapperClassMain {
    public static void main(String[] args) {
        Integer i = new Integer(10);
        Integer in = new Integer(10);
        Integer integerObj = Integer.valueOf(10);  //-128~ 127 자주 사용하는 숫자 값 재사용
    

        System.out.println("내부 값 읽기");
        int intValue = i.intValue();
        System.out.println(intValue);

        System.out.println(i == intValue); //true
        System.out.println(i == integerObj); //false
        System.out.println(i == in); //false
        System.out.println(in == intValue); //true

    }
}

래퍼 클래스는 인스턴스의 참조값을 비교한다고 했는데, (i==intValue) 이 부분에서 참조형과 기본형을 비교하는 부분에서는 왜 true가 나오나요?

java객체지향

Answer 1

0

안녕하세요. 주형님, 공식 서포터즈 David입니다.

자바는 다음과 같은 상황에서 intValue를 언박싱하여 비교합니다. (참고)

i == intValue

감사합니다.

njhzi5114님의 프로필 이미지
njhzi5114
Questioner

inValue는 언박싱이 이미 된 것 아닌가요?? 혹시 i를 언박싱해서 비교한다는 말씀이신가요?

네, 제가 잘못 기재했습니다.

i가 언박싱됩니다.

njhzi5114's profile image
njhzi5114

asked

Ask a question