How do I convert from int to Long in Java?
Use the following: Long.valueOf(int);
.
use
new Long(your_integer);
or
Long.valueOf(your_integer);
Note that there is a difference between a cast to long
and a cast to Long
. If you cast to long
(a primitive value) then it should be automatically boxed to a Long
(the reference type that wraps it).
You could alternatively use new
to create an instance of Long
, initializing it with the int
value.
If you already have the int typed as an Integer you can do this:
Integer y = 1;
long x = y.longValue();