문제

class D {
    public static void main(String args[]) {
        Integer b2=128;
        Integer b3=128;
        System.out.println(b2==b3);
    }
}

산출:

false

class D {
    public static void main(String args[]) {
        Integer b2=127;
        Integer b3=127;
        System.out.println(b2==b3);
    }
}

산출:

true

참고 : -128과 127 사이의 숫자는 사실입니다.

도움이 되었습니까?

해결책

자바로 문자 그대로 문자를 컴파일하고 정수에 할당 할 때 (자본 I) 컴파일러가 방출됩니다.

Integer b2 =Integer.valueOf(127)

이 코드 라인은 오토 옥싱을 사용할 때도 생성됩니다.

valueOf 특정 숫자가 "풀링"되도록 구현되며 128보다 작은 값에 대해 동일한 인스턴스를 반환합니다.

Java 1.6 소스 코드, 라인 621 :

public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

의 가치 high 시스템 속성으로 다른 값으로 구성 할 수 있습니다.

-djava.lang.integer.integercache.high = 999

해당 시스템 속성으로 프로그램을 실행하면 TRUE를 출력합니다!

명백한 결론 : 두 가지 참고 문헌에 동일하게 의존하지 마십시오. 항상 비교하십시오. .equals() 방법.

그래서 b2.equals(b3) B2, B3의 모든 논리적 동등한 값에 대해 true를 인쇄합니다.

주목하십시오 Integer 캐시는 성능의 이유로 존재하지 않고 오히려 JLS, 섹션 5.1.7; 값 -128 ~ 127에 대한 객체 아이덴티티가 제공되어야합니다.

정수#valueof (int) 또한이 행동을 문서화합니다.

이 방법은 자주 요청되는 값을 캐싱하여 훨씬 더 나은 공간과 시간 성능을 산출 할 수 있습니다. 이 방법은 항상 -128 ~ 127의 범위의 값을 포함하며,이 범위를 벗어난 다른 값을 캐시 할 수 있습니다.

다른 팁

오토 옥싱 캐시 -128 ~ 127. 이것은 JLS에 지정되어 있습니다 (5.1.7).

값이라면박스가되는 것은 사실, 거짓, 바이트, 범위 u0000에서 u007f 범위의 숯, 또는 -128과 127 사이의 int 또는 짧은 숫자입니다. 그런 다음 R1과 R2는 p의 두 권투 변환의 결과가됩니다. R1 == R2는 항상 그렇습니다.

개체를 다룰 때 기억해야 할 간단한 규칙은 - 사용입니다. .equals 두 객체가 "동일"인지 확인하려면 사용하십시오. == 동일한 인스턴스를 가리키는 지 확인하고 싶을 때.

Primitive Data Type, Ints를 사용하면 예상 출력 모두에 따라 True가 생성됩니다.

그러나 정수 객체를 사용하기 때문에 == 연산자는 다른 의미를 갖습니다.

객체의 맥락에서 == 변수가 동일한 객체 참조를 참조하는지 확인하기 위해 검사합니다.

객체의 값을 비교하려면 equals () 메소드를 사용해야합니다.

 b2.equals(b1)

B2가 B1 미만인지, 또는 동일인지를 나타냅니다 (자세한 내용은 API를 확인하십시오).

Java 관련 메모리 최적화입니다.

메모리를 저장하려면 Java는 다음 범위에 값이 떨어지는 모든 래퍼 객체를 재사용합니다.

모든 부울 값 (참과 거짓)

모든 바이트 값

u0000에서 u007f의 모든 문자 값 (예 : 10 진수)

모든 짧고 정수 값은 -128에서 127입니다.

값이 -128에서 127 사이 인 경우 integer.java를 살펴보십시오. 캐시 풀을 사용하므로 (Integer) 1 == (Integer) 1 동안 (Integer) 222 != (Integer) 222

 /**
 * Returns an {@code Integer} instance representing the specified
 * {@code int} value.  If a new {@code Integer} instance is not
 * required, this method should generally be used in preference to
 * the constructor {@link #Integer(int)}, as this method is likely
 * to yield significantly better space and time performance by
 * caching frequently requested values.
 *
 * This method will always cache values in the range -128 to 127,
 * inclusive, and may cache other values outside of this range.
 *
 * @param  i an {@code int} value.
 * @return an {@code Integer} instance representing {@code i}.
 * @since  1.5
 */
public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}       

나는이 문제가 정수에만 국한되지 않기 때문에 다음을 썼습니다. 내 결론은 API를 잘못 사용하면 잘못된 행동을 볼 수 있다는 것입니다. 올바르게 사용하면 올바른 동작이 나타납니다.

public static void main (String[] args) {
    Byte b1=127;
    Byte b2=127;

    Short s1=127; //incorrect should use Byte
    Short s2=127; //incorrect should use Byte
    Short s3=128;
    Short s4=128;

    Integer i1=127; //incorrect should use Byte
    Integer i2=127; //incorrect should use Byte
    Integer i3=128;
    Integer i4=128;

    Integer i5=32767; //incorrect should use Short
    Integer i6=32767; //incorrect should use Short

    Long l1=127L;           //incorrect should use Byte
    Long l2=127L;           //incorrect should use Byte
    Long l3=13267L;         //incorrect should use Short
    Long l4=32767L;         //incorrect should use Short
    Long l5=2147483647L;    //incorrect should use Integer 
    Long l6=2147483647L;    //incorrect should use Integer
    Long l7=2147483648L;
    Long l8=2147483648L;

    System.out.print(b1==b2); //true  (incorrect) Used API correctly
    System.out.print(s1==s2); //true  (incorrect) Used API incorrectly
    System.out.print(i1==i2); //true  (incorrect) Used API incorrectly
    System.out.print(l1==l2); //true  (incorrect) Used API incorrectly

    System.out.print(s3==s4); //false (correct) Used API correctly
    System.out.print(i3==i4); //false (correct) Used API correctly
    System.out.print(i5==i6); //false (correct) Used API correctly
    System.out.print(l3==l4); //false (correct) Used API correctly
    System.out.print(l7==l8); //false (correct) Used API correctly
    System.out.print(l5==l6); //false (correct) Used API incorrectly

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top