Java:为什么我不能将int转换为Long [英] Java: Why can't I cast int to Long

查看:325
本文介绍了Java:为什么我不能将int转换为Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

All numbers in Java are supposed to be of int type. The following line is legal in 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

相同的机制适用于整数字节实例化。但Long的工作完全不同。以下代码给出了编译时错误

Same mechanics go for Integer and Byte instantiation. But Long works completely different. The following code gives compile time error

Long l = 10;

Long使用相同的方法进行长类型的自动装箱,所以

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

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

我看不出为什么是int无法分配给Long变量。关于这个问题的任何想法?

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

推荐答案

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

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.

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

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


  • 缩小原语如果变量的类型是byte,short或char,则可以使用转换,并且常量表达式的值可以在变量的类型中表示。

  • 遵循缩小的原始转换如果变量的类型是:

    • 字节,则可以使用装箱转换,并且类型字节中可以表示常量表达式的值。

    • Short并且常量表达式的值可以在short类型中表示。

    • 字符和常量表达式的值可以在char类型中表示。

    • 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".

      因此所有< = 32bit基元都可以轻松地进行转换,长期(64位)需要特别铸造。
      似乎不合逻辑。

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

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

      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:为什么我不能将int转换为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆