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

时间:2023-02-07 02:57:08

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 Integer and Byte instantiation. But Long works completely different. The following code gives compile time error

同样的机制也适用于整数和字节实例化。但长期工作完全不同。下面的代码给出了编译时间错误

Long l = 10;

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

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不能被赋给一个长变量。对这件事有什么想法吗?

4 个解决方案

#1


7  

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 and int to java.lang。短为例。

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

JLS:“另外,如果表达式是一个常量表达式(§15.28)类型的字节,短,char或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.
  • 如果变量的类型是字节、短或char,并且常量表达式的值在变量的类型中是可表示的,则可以使用窄化原语转换。
  • 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.
    • 常量表达式的值可以用Short类型表示。
    • Character and the value of the constant expression is representable in the type char".
    • 字符和常量表达式的值可以在char类型中表示。
  • 如果变量的类型是:Byte并且常量表达式的值在类型Byte中是可表示的,则可以使用收缩原语转换,然后是装箱转换。常量表达式的值可以用Short类型表示。字符和常量表达式的值可以在char类型中表示。

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

因此,所有的<=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的向后压缩或历史演进中有解释。例如,自1.0版本以来,java中一直存在整数类和Long类。从1.1开始,java的类短和字节都存在。那就是在起点积分数可以有两种类型:整数或长。所以我认为这两种类型的数字有不同的铸造规则。然后添加短字节。我认为在具体的jvm中,short和byte可以有32位的实现。

#2


2  

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

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

Take a look here .

看看这里。

#3


0  

You can cast int to long and long to Long
but you can't cast int to Long

你可以将int类型转换成long类型,也可以将int类型转换成long类型,但不能将int类型转换成long类型

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

写长l =(长)10是正确的;

#4


0  

1,new Long(intValue);

1、新长(intValue);

2,Long.valueOf(intValue);

2,Long.valueOf(intValue);

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

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

#1


7  

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 and int to java.lang。短为例。

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

JLS:“另外,如果表达式是一个常量表达式(§15.28)类型的字节,短,char或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.
  • 如果变量的类型是字节、短或char,并且常量表达式的值在变量的类型中是可表示的,则可以使用窄化原语转换。
  • 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.
    • 常量表达式的值可以用Short类型表示。
    • Character and the value of the constant expression is representable in the type char".
    • 字符和常量表达式的值可以在char类型中表示。
  • 如果变量的类型是:Byte并且常量表达式的值在类型Byte中是可表示的,则可以使用收缩原语转换,然后是装箱转换。常量表达式的值可以用Short类型表示。字符和常量表达式的值可以在char类型中表示。

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

因此,所有的<=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的向后压缩或历史演进中有解释。例如,自1.0版本以来,java中一直存在整数类和Long类。从1.1开始,java的类短和字节都存在。那就是在起点积分数可以有两种类型:整数或长。所以我认为这两种类型的数字有不同的铸造规则。然后添加短字节。我认为在具体的jvm中,short和byte可以有32位的实现。

#2


2  

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

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

Take a look here .

看看这里。

#3


0  

You can cast int to long and long to Long
but you can't cast int to Long

你可以将int类型转换成long类型,也可以将int类型转换成long类型,但不能将int类型转换成long类型

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

写长l =(长)10是正确的;

#4


0  

1,new Long(intValue);

1、新长(intValue);

2,Long.valueOf(intValue);

2,Long.valueOf(intValue);

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

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