gpt4 book ai didi

java - 为什么不隐式转换范围内缩小的长值?

转载 作者:搜寻专家 更新时间:2023-10-31 20:19:49 25 4
gpt4 key购买 nike

当我们声明一个 static final 时,Java 编译器(或预编译器?)似乎足够智能以检测超出范围的数字:

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;
}

上面的代码证明,对于intshortchar 值,编译器只会在值超出-分配变量类型的范围。

但是对于 long 值,即使数字在范围内,编译器也会报错:

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;
}

为什么编译器只对 intshortchar 值进行范围检测?

为什么编译器不对 long 值进行范围检测?

最佳答案

答案可能不尽如人意,但是...

Java Language Specification, Section 5.2 , 说:

Assignment contexts allow the value of an expression to be assigned (§15.26) to a variable; the type of the expression must be converted to the type of the variable.

...

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

对于编译良好的情况,常量表达式始终是shortcharint类型,并且值是可表示的在目标类型中。对于 long 类型,根据规范根本不允许这样的转换。


答案可能并不令人满意,因为下一个显而易见的问题是:

Why did they write the specification like that?

这可能部分地通过 JLS 的链接部分中给出的示例来回答:这种隐式转换很可能主要用于您想要编写如下声明的情况

byte b = 42;

否则,您必须将 int 值 42 转换为 byte,如

byte b = (byte)42;

从这个意义上说,您想从 long 值初始化 byte 的情况并不常见。

关于java - 为什么不隐式转换范围内缩小的长值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com