When we declare static final , the Java compiler (or the preliminary compiler?) Seems smart enough to detect numbers out of range:
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; }
The above code proves that for int , short and char values, the compiler only complains when the value is out of range for the type of the assigned variable.
However, for long values, the compiler complains even if the numbers are within the range:
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;
Why is the compiler only smart when it detects a range for int , short and char ?
Why doesn't the compiler detect a range for long values?
source share