How do I convert from int to Long in Java?

677,629

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);
Share:
677,629

Related videos on Youtube

Ghosty
Author by

Ghosty

Updated on October 09, 2021

Comments

  • Ghosty
    Ghosty over 2 years

    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.

    • Instantsoup
      Instantsoup over 14 years
      for (long i = 0; i < myList.size(); ++i) works too
    • Ghosty
      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
      Daniel Jipa over 6 years
      long longValue = intValue | 0L;
    • Eric
      Eric almost 5 years
      What I did is: cast int to long first, then assign long to Long. e.g int x = 2; Long y = (long)x;
  • Grundlefleck
    Grundlefleck over 14 years
    To 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
    palantus over 14 years
    Autoboxing is preferable, because it doesn't necessarily have to create a new Long object every time.
  • Grundlefleck
    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
    Daniel Earwicker over 14 years
    Autoboxing does the same thing as that. By the way, it's between -127 and 128.
  • palantus
    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
    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
    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
    will824 over 12 years
    Look out, as this will generate a NullPointerException if you are receiving a Integer object which is null.
  • Daniel Earwicker
    Daniel Earwicker over 12 years
    if jPaidCountSpinner.getValue() returns an Object that is in fact a Long, you definitely only need to put a (Long) cast in front. Also try putting a breakpoint on your check for int.class or long.class etc. Does it ever hit it? And if you have a number-like object, it will support java.util.Number, so cast it to that and call the longValue 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 for JSpinner).
  • shareef
    shareef almost 10 years
    correct if am wron i thought may be the answer is old becuse i cant find the method Long.valueOf(int) !!
  • Rondo
    Rondo over 9 years
    @will824 - a primitive 'int' variable cannot be null.
  • Rondo
    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
    Salem about 7 years
    @Rondo an Integer can, which is what he said.
  • Rondo
    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
    Profiterole almost 6 years
    I 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
    Martin Schröder over 4 years
    longValue is just a (long), so the only thing you "gain" here is Integer's internal cache.
  • Farid
    Farid about 4 years
    There is no such method in Java, you mistook it for Long.valueOf(long)
  • syd
    syd over 3 years
    yeah. how about just (long) intValue ?