在 java 中将 int 文字分配给 Long
assigning int literal to Long in java
我看不出发生这种情况的明确理由 ..
public class wrappers
{
public static void main(String []args)
{
short s1=32767; // (primitive) works fine without compile error
short s2=32768; fails as this is beyond short's range
Short s3=32767; //(wrapper) works fine without compile error
Short s4=32768; fails as this is beyond short's range
long l1 =34 // (wrapper)works fine (with_in_range)
Long l2 =34 // fails, without 34L (with_in_range)
}
我知道当您为包装器 类 分配 int
文字时,会调用 valueOf()
;
虽然对于 Short(Wrapper)
这似乎可行,但对于 Long(wrapper)
以上代码中的分配失败。
这些包装器 类 的分配是否有任何规则?
are there any rules governing these wrapper classes' assignments
是的 - JLS 的规则。它指定 widening primitive conversions (5.1.2) and boxing conversions (5.1.7).
拳击转换列表包括:
- From type
int
to type Integer
- From type
long
to type Long
因此您需要先进行加宽转换,然后进行装箱转换。您正在尝试在 作业上下文 (5.2) 中执行此操作,其中说明:
Assignment contexts allow the use of one of the following:
- an identity conversion (§5.1.1)
- a widening primitive conversion (§5.1.2)
- a widening reference conversion (§5.1.5)
- a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
- an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.
请注意,此列表不包括一个加宽的基元转换,然后是一个装箱转换。反过来也行:
Integer x = 10;
long y = x; // Unboxing followed by widening primitive
从表面上看,规则 可能 已被编写为允许扩大基元转换,然后进行装箱转换,规则如下 Object x = 10;
更喜欢装箱,因为 Integer
而不是 Long
... 但是类型转换规则 非常 很难正确,有很多微妙之处。如果事实证明有一些奇怪的情况会导致大问题,我不会感到惊讶。
转换为 Short
的原因是 5.2 中的进一步规则:
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.
- A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte
and the value of the constant expression is representable in the type byte
.
Short
and the value of the constant expression is representable in the type short
.
Character
and the value of the constant expression is representable in the type char
.
如
Short s = 32767;
有一个缩小的原始转换,然后是如上所述的装箱转换。
自动装箱仅在原始类型和它们各自的包装器之间完成 类 如:
int to Integer,
long to Long,
double to Double and so on.
Java 不允许基本类型 int 和 Object Long 之间的转换。
但是,它确实允许原始类型 int 和 long 之间的转换。
因此,如果您需要将 int 分配给 Long 类型的变量,则需要添加后缀 l 或 L:
Long l2 =34L,
为了扩展 Jon 的回答,JLS 中还指定了允许 Short s3=32767
工作的规则。关于常量表达式的赋值有一个特殊规则:
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.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
我看不出发生这种情况的明确理由 ..
public class wrappers
{
public static void main(String []args)
{
short s1=32767; // (primitive) works fine without compile error
short s2=32768; fails as this is beyond short's range
Short s3=32767; //(wrapper) works fine without compile error
Short s4=32768; fails as this is beyond short's range
long l1 =34 // (wrapper)works fine (with_in_range)
Long l2 =34 // fails, without 34L (with_in_range)
}
我知道当您为包装器 类 分配 int
文字时,会调用 valueOf()
;
虽然对于 Short(Wrapper)
这似乎可行,但对于 Long(wrapper)
以上代码中的分配失败。
这些包装器 类 的分配是否有任何规则?
are there any rules governing these wrapper classes' assignments
是的 - JLS 的规则。它指定 widening primitive conversions (5.1.2) and boxing conversions (5.1.7).
拳击转换列表包括:
- From type
int
to typeInteger
- From type
long
to typeLong
因此您需要先进行加宽转换,然后进行装箱转换。您正在尝试在 作业上下文 (5.2) 中执行此操作,其中说明:
Assignment contexts allow the use of one of the following:
- an identity conversion (§5.1.1)
- a widening primitive conversion (§5.1.2)
- a widening reference conversion (§5.1.5)
- a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
- an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.
请注意,此列表不包括一个加宽的基元转换,然后是一个装箱转换。反过来也行:
Integer x = 10;
long y = x; // Unboxing followed by widening primitive
从表面上看,规则 可能 已被编写为允许扩大基元转换,然后进行装箱转换,规则如下 Object x = 10;
更喜欢装箱,因为 Integer
而不是 Long
... 但是类型转换规则 非常 很难正确,有很多微妙之处。如果事实证明有一些奇怪的情况会导致大问题,我不会感到惊讶。
转换为 Short
的原因是 5.2 中的进一步规则:
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
, orchar
, and the value of the constant expression is representable in the type of the variable.- A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte
and the value of the constant expression is representable in the typebyte
.Short
and the value of the constant expression is representable in the typeshort
.Character
and the value of the constant expression is representable in the typechar
.
如
Short s = 32767;
有一个缩小的原始转换,然后是如上所述的装箱转换。
自动装箱仅在原始类型和它们各自的包装器之间完成 类 如:
int to Integer,
long to Long,
double to Double and so on.
Java 不允许基本类型 int 和 Object Long 之间的转换。 但是,它确实允许原始类型 int 和 long 之间的转换。 因此,如果您需要将 int 分配给 Long 类型的变量,则需要添加后缀 l 或 L:
Long l2 =34L,
为了扩展 Jon 的回答,JLS 中还指定了允许 Short s3=32767
工作的规则。关于常量表达式的赋值有一个特殊规则:
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.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.