Java:为什么我不能将 int 转换为 Long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7765874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:19:36  来源:igfitidea点击:

Java: Why can't I cast int to Long

javaprimitive-types

提问by SirVaulterScoff

All numbers in Java are supposed to be of int type. The following line is legal in Java>1.5

Java 中的所有数字都应该是 int 类型。以下行在 Java>1.5 中是合法的

Short s = 1; // Will compile to Short s = Short.valueOf((short)1) - thus you can't exceed short max value i.e.
Short s =  4444; // is invalid for autoboxing

Same mechanics go for Integerand Byteinstantiation. But Long works completely different. The following code gives compile time error

相同的机制IntegerByte实例化。但 Long 的工作方式完全不同。以下代码给出了编译时错误

Long l = 10;

Long uses the same approach for autoboxing of long types, so

Long 使用相同的方法对 long 类型进行自动装箱,因此

Long l = 10L; //is valid and is translated into Long.valueOf(10L)

I can't see why int cannot be assigned to a Long variable. Any thoughts on this matter?

我不明白为什么不能将 int 分配给 Long 变量。对这个问题有什么想法吗?

采纳答案by svaor

I think the question was not about casting primitives and wrappers in general. The question was about difference between casting int to java.lang.Long and int to java.lang.Short for example.

我认为问题不在于一般的铸造原语和包装器。例如,问题是关于将 int 转换为 java.lang.Long 和将 int 转换为 java.lang.Short 之间的区别。

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

JLS:“此外,如果表达式是 byte、short、char 或 int 类型的常量表达式(第 15.28 节):

  • 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".
  • 如果变量的类型是 byte、short 或 char,并且常量表达式的值可以在变量的类型中表示,则可以使用缩小原语转换。
  • 如果变量的类型是:
    • 字节和常量表达式的值可以在类型字节中表示。
    • Short 和常量表达式的值可以在类型 short 中表示。
    • 字符和常量表达式的值可以在类型 char" 中表示。

So all <=32bit primitives can be casted easily and long (64bit) requires special casting. It seems illogically.

因此,所有 <=32 位原语都可以轻松转换,并且长(64 位)需要特殊转换。这似乎不合逻辑。

All illogical things as usual has explanation in backward compability or historical evolution in java. E.g. classes Integer and Long exist in java since version 1.0. Classes Short and Byte exist in java since 1.1. That is at the start point integral number can be two types: integer or long. So I think there are different casting rules for these two types of numbers. And then short and byte were added. I suppose short and byte can have 32-bit implementation in concrete JVMs.

所有不合逻辑的事情,在java中都有向后兼容性或历史演变的解释。例如 Integer 和 Long 类从 1.0 版开始就存在于 Java 中。从 1.1 开始,Java 中就存在类 Short 和 Byte。即在起点的整数可以有两种类型:整数或长整数。所以我认为这两种类型的数字有不同的转换规则。然后添加了 short 和 byte。我想 short 和 byte 在具体的 JVM 中可以有 32 位实现。

回答by aleroot

Because Long with the first capital letter is a wrapper class and not the primitive type .

因为第一个大写字母的 Long 是一个包装类而不是原始类型。

Take a look here.

看看这里

回答by NtheP

You can cast intto longand longto Long
but you can't cast intto Long

你可以投射intlonglong到,Long
但你不能投射intLong

it is correct to write Long l = (long) 10;

写是正确的 Long l = (long) 10;

回答by Vicky

1,new Long(intValue);

1,new Long(intValue);

2,Long.valueOf(intValue);

2、Long.valueOf(intValue);

https://stackoverflow.com/a/50225345/9744452

https://stackoverflow.com/a/50225345/9744452