Java:Long result = -1:无法从int转换为long [英] Java: Long result = -1: cannot convert from int to long
问题描述
我使用eclipse java ee执行java编程。
我在我的一个函数中有以下代码行:
长结果= -1;
我收到以下错误:
类型不匹配:无法从int转换为长
我不明白为什么当我添加
如何解决这个问题,为什么会在第一时间发生?
int
所以你需要从 long
。添加 L
使整数 -1
变成 long
( -1L
): 长结果= -1L;
但是从 int
long
所以这样工作:
long result = -1;
因此,您可以这样写:
长结果=(长)-1;
从原始( int
code>,,
,
long
等)转换为Wrapper对象)称为自动装箱,请阅读此处。
I'm using eclipse java ee to perform java programming.
I had the following line of code in one of my functions:
Long result = -1;
I got the following error:
Type mismatch: cannot convert from int to Long
I can't quite understand why when i add a number to the variable it provides this error.
How can this issue be resolved and why did it happen in the first place?
There is no conversion between the object Long
and int
so you need to make it from long
. Adding a L
makes the integer -1
into a long
(-1L
):
Long result = -1L;
However there is a conversion from int
a long
so this works:
long result = -1;
Therefore you can write like this aswell:
Long result = (long) -1;
Converting from a primitive (int
, long
etc) to a Wrapper object (Integer
, Long
etc) is called autoboxing, read more here.
这篇关于Java:Long result = -1:无法从int转换为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!