views:

20071

answers:

5

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!

+17  A: 

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.

Daniel Earwicker
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.
Grundlefleck
Autoboxing is preferable, because it doesn't necessarily have to create a new `Long` object every time.
Michael Myers
(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...)
Grundlefleck
Autoboxing does the same thing as that. By the way, it's between -127 and 128.
Daniel Earwicker
@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.
Michael Myers
And watch out for accidentally writing `a == b` to compare two boxed values! Will appear to work if they're in the "interning" range, will fail if not. And the range is a *minimum* of -128 to 127, but may be larger. (I think that's right, looks like I typed it the wrong way round in my last comment).
Daniel Earwicker
@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? :-)
Grundlefleck
@Grundlefleck - thanks, but would probably make more sense on a question about what autoboxing is equivalent to, e.g. you could upvote this answer: http://stackoverflow.com/questions/766468/autoboxing-so-i-can-write-integer-i-0-instead-of-integer-i-new-integer0/767167#767167
Daniel Earwicker
A: 

have you tried casting the int like so;

int intnumber = 1;
Long longnumber = (Long)(long)intnumber;
instanceofTom
Actually, you can get away without the "`(Long)`" too.
Michael Myers
@Tnay: The code above only works in 1.5+ due to autoboxing and the cast to `Long` is unnecessary.
Grant Wagner
@Grant: "only works with 1.5+"? Java 5 is [6 years old](http://en.wikipedia.org/wiki/Java_version_history#J2SE_5.0_.28September_30.2C_2004.29) so I think we no longer need these disclaimers ;-)
Joachim Sauer
+4  A: 

Long.valueOf(int);

serg
Is there such a method?
Tom Hawtin - tackline
There is Long.valueOf(long) which works for int as well.
serg
A: 

If you already have the int typed as an Integer you can do this:

Integer y = 1;
long x = y.longValue();
saret
+1  A: 

I had a great deal of trouble with this. I just wanted to:

thisBill.IntervalCount = jPaidCountSpinner.getValue();

Where IntervalCount is a Long, and the JSpinner was set to return a Long. Eventually I had to write this function:

    public static final Long getLong(Object obj) throws IllegalArgumentException {
    Long rv;

    if((obj.getClass() == Integer.class) || (obj.getClass() == Long.class) || (obj.getClass() == Double.class)) {
        rv = Long.parseLong(obj.toString());
    }
    else if((obj.getClass() == int.class) || (obj.getClass() == long.class) || (obj.getClass() == double.class)) {
        rv = (Long) obj;
    }
    else if(obj.getClass() == String.class) {
        rv = Long.parseLong(obj.toString());
    }
    else {
        throw new IllegalArgumentException("getLong: type " + obj.getClass() + " = \"" + obj.toString() + "\" unaccounted for");
    }

    return rv;
}

which seems to do the trick. No amount of simple casting, none of the above solutions worked for me. Very frustrating.

MaskedCoder