I keep finding both on here and Google people having troubles going from long
to int
and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int
to Long
.
The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question.
Can someone help me out here? I initially tried casting but I get a "Cannot cast from int to Long
"
for (int i = 0; i < myArrayList.size(); ++i ) {
content = new Content();
content.setDescription(myArrayList.get(i));
content.setSequence((Long) i);
session.save(content);
}
As you can imagine I'm a little perplexed, I'm stuck using int since some content is coming in as an ArrayList and the entity for which I'm storing this info requires the sequence number as a Long.
Thanks!
Note that there is a difference between a cast to
long
and a cast toLong
. If you cast tolong
(a primitive value) then it should be automatically boxed to aLong
(the reference type that wraps it).You could alternatively use
new
to create an instance ofLong
, initializing it with theint
value.Use the following:
Long.valueOf(int);
.use
or
In Java you can do:
in your case it would be:
We shall get the long value by using
Number
reference.It works for all number types, here is a test: