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

后端 未结 7 803
你的背包
你的背包 2020-12-03 19:39

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;

7条回答
  •  温柔的废话
    2020-12-03 20:10

    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.

提交回复
热议问题