Java:长结果 = -1:无法从 int 转换为 long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4437358/
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 06:22:08  来源:igfitidea点击:

Java: Long result = -1: cannot convert from int to long

javacasting

提问by ufk

I'm using eclipse java ee to perform java programming.

我正在使用 eclipse java ee 来执行 java 编程。

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

类型不匹配:无法从 int 转换为 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?

如何解决这个问题,为什么会发生?

回答by dacwe

There is no conversion between the object Longand intso you need to make it from long. Adding a Lmakes the integer -1into a long(-1L):

对象之间没有转换Longint因此您需要从long. 添加 aL使整数-1变为 a long( -1L):

Long result = -1L;

However there is a conversion from inta longso this works:

但是有一个从inta的转换,long所以这是有效的:

long result = -1;

Therefore you can write like this aswell:

因此你也可以这样写:

Long result = (long) -1;

Converting from a primitive (int, longetc) to a Wrapper object (Integer, Longetc) is called autoboxing, read more here.

从一个原语(转换intlong等),以一个包装对象(IntegerLong等)被称为自动装箱,阅读更多这里

回答by Matthew Flaschen

-1can be auto-boxed to an Integer.

-1可以自动装箱为整数。

Thus:

因此:

Integer result = -1;

works and is equivalent to:

工作,相当于:

Integer result = Integer.valueOf(-1);

However, an Integer can't be assigned to a Long, so the overall conversion in the question fails.

但是,不能将 Integer 分配给 Long,因此问题中的整体转换失败。

Long result = Integer.valueOf(-1);

won't work either.

也行不通。

If you do:

如果你这样做:

Long result = -1L;

it's equivalent (again because of auto-boxing) to:

它等效于(再次因为自动装箱):

Long result = Long.valueOf(-1L);

Note that the Lmakes it a longliteral.

请注意,这L使它成为long文字。

回答by David Peleg

First of all, check you Java version: run "java -version" from command line.

首先,检查您的 Java 版本:从命令行运行“java -version”。

In Java version prior to 1.5, the correct syntax is:

在 1.5 之前的 Java 版本中,正确的语法是:

Long result = new Long(1L);

Long 结果 = new Long(1L);

In java >1.5 it's done automatically. It's called "autoboxing".

在 java > 1.5 中它是自动完成的。这被称为“自动装箱”。

The uppercase "L" tells Java that the number should be interpreted as long.

大写的“L”告诉 Java 这个数字应该被解释为 long。

回答by Thilo

 Long result = -1L;

 Long result = (long) -1;

 Long result
     = Long.valueOf(-1);  // this is what auto-boxing does under the hood

How it happened in the first place is that the literal -1is an int. As of Java5, you can have it auto-boxed into an Integer, when you use it as on Object, but it will not be turned into a Long(only longs are).

它首先是如何发生的,因为文字-1是一个int. 从 Java5 开始Integer,当您将其用作 on 时,您可以将其自动装箱为 an Object,但它不会变成 a Long(只有longs 是)。

Note that an int is automatically widened into a long when needed:

请注意,int 会在需要时自动扩展为 long:

long result = -1;   // this is okay

The opposite way needs an explicit cast:

相反的方式需要显式转换:

int number = (int) result;

回答by Progman

-1is a primitive int value, Longhowever is the wrapper class java.lang.Long. An primitive integer value cannot be assigned to an Long object. There are two ways to solve this.

-1是一个原始的 int 值,Long然而是包装类java.lang.Long。不能将原始整数值分配给 Long 对象。有两种方法可以解决这个问题。

  1. Change the type to long

    If you change the type from the wrapper class Longto the primitive type longjava automatically casts the integer value to a long value.

    long result = -1;

  2. Change the int value to long

    If you change the integer value from -1to -1Lto explicit change it to an long literal java can use this primitive value to automatically wrap this value to an java.lang.Longobject.

    Long result = -1L;

  1. 将类型更改为 long

    如果将类型从包装类Long更改为原始类型,longjava 会自动将整数值转换为长值。

    long result = -1;

  2. 将 int 值更改为 long

    如果将整数值从-1to-1L显式更改为长字面量,java 可以使用此原始值自动将此值包装到一个java.lang.Long对象中。

    Long result = -1L;

回答by Progman

Long result = -1L;

回答by Amber

To specify a constant of type long, suffix it with an L:

要指定类型的常量long,请为其添加后缀L

Long result = -1L;

Without the L, the interpreter types the constant number as an int, which is not the same type as a long.

如果没有L,解释器将常量数字int键入为an ,这与 a 的类型不同long