2014-09-08 5 views
5

우리는 static final, 자바 컴파일러를 선언 범위를 벗어난 숫자를 감지 할만큼 똑똑 보인다왜 범위 내에서 long 값이 암시 적으로 변환되지 않았습니까? (? 또는 사전 컴파일러)

public class Test { 
    // setup variables: 
    public static final int i_max_byte = 127; 
    public static final int i_max_byte_add1 = 128; 
    public static final int i_max_short = 32767; 
    public static final int i_max_short_add1 = 32768; 
    public static final int i_max_char = 65535; 
    public static final int i_max_char_add1 = 65536; 
    public static final char c_max_byte = 127; 
    public static final char c_max_byte_add1 = 128; 
    public static final char c_max_short = 32767; 
    public static final char c_max_short_add1 = 32768; 
    public static final short s_min_char = 0; 
    public static final short s_min_char_sub1 = -1; 
    public static final short s_max_byte = 127; 
    public static final short s_max_byte_add1 = 128; 

    // all these are OK: 
    public static final byte b1 = i_max_byte; 
    public static final byte b2 = s_max_byte; 
    public static final byte b3 = c_max_byte; 
    public static final byte b4 = (short) i_max_byte; 
    public static final byte b5 = (char) i_max_byte; 
    public static final char c1 = i_max_char; 
    public static final char c2 = s_min_char; 
    public static final short s1 = i_max_short; 
    public static final short s2 = c_max_short; 

    // pre-compiler complains "type-mismatch": 
    public static final byte _b1 = i_max_byte_add1; 
    public static final byte _b2 = s_max_byte_add1; 
    public static final byte _b3 = c_max_byte_add1; 
    public static final byte _b4 = (short) i_max_byte_add1; 
    public static final byte _b5 = (char) i_max_byte_add1; 
    public static final char _c1 = i_max_char_add1; 
    public static final char _c2 = s_min_char_min_us1; 
    public static final short _s1 = i_max_short_add1; 
    public static final short _s2 = c_max_short_add1; 
} 

위의 코드는 증명이 int, shortchar 값 , 컴파일러는 값이 지정된 변수의 유형에 대해 범위를 벗어날 경우에만 불평합니다.

public class Test2 { 
    public static final long l_max_byte = 127; 
    public static final long l_max_byte_add1 = 128; 
    public static final long l_max_char = 32767; 
    public static final long l_max_char_add1 = 32768; 
    public static final long l_max_short = 65535; 
    public static final long l_max_short_add1 = 65536; 
    public static final long l_max_int = 2147483647; 
    public static final long l_max_int_add1 = 2147483648L; 

    // "type-mismatch" for all: 
    public static final byte b1 = l_max_byte; 
    public static final byte b2 = l_max_byte_add1; 
    public static final char c1 = l_max_char; 
    public static final char c2 = l_max_char_add1; 
    public static final short s1 = l_max_short; 
    public static final short s2 = l_max_short_add1; 
    public static final int i1 = l_max_int; 
    public static final int i2 = l_max_int_add1; 
} 

char 값 컴파일러 int, short에 대한 범위 감지에서만 똑똑하고 : 숫자가 범위 내에있을 때

그러나 long 값, 컴파일러도 불만?

컴파일러가 long 값의 범위 감지를 수행하지 않는 이유는 무엇입니까?

+0

이전 Java 디자인으로 인해 제한된 범위 (-128 - 127)에서만 컴파일됩니다. 와이드닝 그냥 작품을 찾으십시오. – Hannes

+0

@Hannes, Yea 넓히는 것은 괜찮습니다 (잘 지내기 때문에, 항상 범위 내입니다). 이상하게 좁은 부분입니다. 어떤 식 으로든 범위 검색은 좁혀지는 값이'int','short' 또는'char' 타입 일 때만 활성화됩니다. – Pacerier

답변

3

대답은 ...

Java Language Specification, Section 5.2 만족 될지도 모르지만, 말한다

할당 상황이 식의 값이 변수 (§15.26)가 할당 될 수 있도록 ; 표현식의 유형은 변수의 유형으로 변환되어야합니다.

...

는 또한 표현 유형 바이트, 짧은 문자의 정수 식 (§15.28) 또는 경우 : INT

  • 좁아 프리미티브 변환하는 경우 사용될 수있다 변수의 유형은 byte, short 또는 char이며, 상수 표현식의 값은 변수 유형으로 나타낼 수 있습니다. 미세 컴파일 경우에

상수 식 항상 형 short, char 또는 int이며, 값은 대상 타입에서 표현할 수있다. long 유형의 경우 이러한 변환은 사양에 따라 허용되지 않습니다. 다음 명백한 질문이기 때문에


대답은 만족스럽지 않을 수 있습니다

왜 그들이 그와 같은 사양을 쓰기 않았다

?

부분적 또한 JLS의 링크 된 구역에 예 응답 할 수있다 :이 암시 적 변환은 대부분 주로 경우를위한 당신이

byte b = 42; 
같은 선언을 작성하려는 즉

그렇지 않으면 때문에, 당신은

byte b = (byte)42; 

당신이초기화하려는 경우와 같이 바이트로 int 값 (42)을 캐스팅해야 이런 의미에서 long 값의은 그리 일반적이지 않습니다.