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

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.

Leave a Comment