Wenn wir ein static final
, die Java-Compiler erklären scheint intelligent genug, um Out-of-Range-Nummern zu erkennen:Warum werden im Bereich beschränkte lange Werte nicht implizit konvertiert? (? Oder Pre-Compiler)
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;
}
Der Code beweist darüber für int
, short
und char
Werte Der Compiler beschwert sich nur, wenn der Wert für den Typ der zugewiesenen Variablen außerhalb des zulässigen Bereichs liegt.
jedoch für long
Werte, beschwert sich der Compiler selbst wenn die Zahlen in Reichweite sind:
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;
}
Warum ist der Compiler nur smart auf Range-Erkennung für int
, short
und char
Werte?
Warum verwendet der Compiler keine Range-Erkennung für long
Werte?
Die Kompilierung macht dies aufgrund des älteren Java-Designs und nur in begrenzten Bereichen (-128 - 127). Verbreiterung funktioniert aber nur. – Hannes
@Hannes, ja Erweiterung ist in Ordnung (da es gut ist, immer innerhalb des Bereichs). Es ist die Verengung, die merkwürdig ist. Irgendwie ist die Bereichserkennung nur dann aktiviert, wenn der zu verengende Wert vom Typ "int", "short" oder "char" ist. – Pacerier