How do I convert from int to Long in Java?

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.

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.

14 Answers
14

Leave a Comment