How do I convert from int to Long in Java?
Solution 1
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.
Solution 2
Use the following: Long.valueOf(int);
.
Solution 3
If you already have the int typed as an Integer you can do this:
Integer y = 1;
long x = y.longValue();
Solution 4
use
new Long(your_integer);
or
Long.valueOf(your_integer);
Solution 5
1,new Long(intValue);
2,Long.valueOf(intValue);
Related videos on Youtube
Ghosty
Updated on October 09, 2021Comments
-
Ghosty over 2 years
I keep finding both on here and Google people having troubles going from
long
toint
and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going fromint
toLong
.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 anArrayList
and the entity for which I'm storing this info requires the sequence number as a Long.-
Instantsoup over 14 yearsfor (long i = 0; i < myList.size(); ++i) works too
-
Ghosty over 14 years@Instantsoup Yes that will work for the loop, but as I specified at the bottom of my question the ArrayList I'm working with requires the primitive int when specifying what index position to get
-
Daniel Jipa over 6 yearslong longValue = intValue | 0L;
-
Eric almost 5 yearsWhat I did is: cast
int
tolong
first, then assignlong
toLong
. e.gint x = 2; Long y = (long)x;
-
-
Grundlefleck over 14 yearsTo demonstrate what Earwicker said you could call Long.valueOf(i), which takes a long but will widen an int and give you back a Long object.
-
palantus over 14 yearsAutoboxing is preferable, because it doesn't necessarily have to create a new
Long
object every time. -
Grundlefleck over 14 years(Warning: the rest of this comment is guesswork and conjecture) If the values given to Long.valueOf() fall between 0 and 128, which is very common, and it returns a cached instance, will that be preferable over autoboxing? (I may ask a new question if you think it's worth it...)
-
Daniel Earwicker over 14 yearsAutoboxing does the same thing as that. By the way, it's between -127 and 128.
-
palantus over 14 years@Grundlefleck: Autoboxing uses
Long.valueOf()
(if I remember correctly), so there wouldn't be a difference at all. My comment was in reply to the answer, not to your comment. -
Grundlefleck over 14 years@mmyers: ah I see, cheers. @Earwicker: may I be so bold to suggest there's some information in these comments that would earn your answer at least another +1 were it to be included? :-)
-
Daniel Earwicker over 14 years@Grundlefleck - thanks, but would probably make more sense on a question about what autoboxing is equivalent to, e.g. you could upvote this answer: stackoverflow.com/questions/766468/…
-
will824 over 12 yearsLook out, as this will generate a NullPointerException if you are receiving a Integer object which is null.
-
Daniel Earwicker over 12 yearsif
jPaidCountSpinner.getValue()
returns an Object that is in fact aLong
, you definitely only need to put a(Long)
cast in front. Also try putting a breakpoint on your check forint.class
orlong.class
etc. Does it ever hit it? And if you have a number-like object, it will supportjava.util.Number
, so cast it to that and call thelongValue
method. No need to go via a string in that case. So this function could be simplified quite a bit, even if you also need to deal with strings. (Part of the problem here is the bad reference documentation forJSpinner
). -
shareef almost 10 yearscorrect if am wron i thought may be the answer is old becuse i cant find the method
Long.valueOf(int) !!
-
Rondo over 9 years@will824 - a primitive 'int' variable cannot be null.
-
Rondo over 9 years@shareef - see serg's comment - but I think this method casts the int to a long which autoboxes with a Long... so seems redundant
-
Salem about 7 years@Rondo an
Integer
can, which is what he said. -
Rondo about 7 years@1blustone either way, it is a red herring: if an Integer was received then a nullpointer would not be generated by this call but by the attempt to get the number into the required primitive: long (Long.valueOf(int) doesn't exists)
-
Profiterole almost 6 yearsI too ended up doing something like this. I wasn't sure what kind of number I would receive and was willing to convert it to Long with little concern for efficiency but wanting to avoid boilerplate.
-
Martin Schröder over 4 years
longValue
is just a(long)
, so the only thing you "gain" here isInteger
's internal cache. -
Farid about 4 yearsThere is no such method in Java, you mistook it for
Long.valueOf(long)
-
syd over 3 yearsyeah. how about just
(long) intValue
?