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

7

5

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?

Cheddite answered 14/12, 2010 at 8:50 Comment(0)
E
15

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.

Elaterid answered 14/12, 2010 at 8:51 Comment(1)
+1, and its important to be uppercase. -1l looks like -11 :)Swordcraft
I
4

-1 can be auto-boxed to an Integer.

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.

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 L makes it a long literal.

Innumerable answered 14/12, 2010 at 8:52 Comment(0)
L
2

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

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

Long result = new Long(1L);

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

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

Laboy answered 14/12, 2010 at 8:54 Comment(1)
Long.valueOf(1L); is better than new Long(1L);, because it can give you a cached object. See API docs of Long.valueOf(long) for more information.Larina
I
1
Long result = -1L;
Inflammatory answered 14/12, 2010 at 8:52 Comment(0)
J
1
 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 -1 is 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).

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

long result = -1;   // this is okay

The opposite way needs an explicit cast:

int number = (int) result;
Jellybean answered 14/12, 2010 at 8:54 Comment(0)
R
1

-1 is a primitive int value, Long however 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. Change the type to long

    If you change the type from the wrapper class Long to the primitive type long java 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 -1 to -1L to explicit change it to an long literal java can use this primitive value to automatically wrap this value to an java.lang.Long object.

    Long result = -1L;

Ramrod answered 14/12, 2010 at 8:59 Comment(0)
M
0

To specify a constant of type long, suffix it with an 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.

Moffett answered 14/12, 2010 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.