Ghosty
Ghosty

Reputation: 3223

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.

Upvotes: 258

Views: 774437

Answers (14)

Shirish Singh
Shirish Singh

Reputation: 857

Apart from the other ways suggested here, one can try the below code as well.

(long)intValue

primitive to primitive.

Upvotes: 2

Noor Hossain
Noor Hossain

Reputation: 1841

Suggested From Android Studio lint check : Remove Unnecessary boxing : So, unboxing is :

public  static  long  integerToLong (int minute ){
    int delay = minute*1000;
    long diff = (long) delay;
    return  diff ; 
}

Upvotes: 2

Vikki
Vikki

Reputation: 2025

 1,new Long(intValue);
 2,Long.valueOf(intValue);

Upvotes: 11

Stanislav Tsepa
Stanislav Tsepa

Reputation: 720

As soon as there is only method Long.valueOf(long), cast from int to long will be done implicitly in case of using Long.valueOf(intValue).

The more clear way to do this is

Integer.valueOf(intValue).longValue()

Upvotes: 0

Virendra Singh
Virendra Singh

Reputation: 297

 //Suppose you have int and you wan to convert it to Long
 int i=78;
 //convert to Long
 Long l=Long.valueOf(i)

Upvotes: 2

Steven Spungin
Steven Spungin

Reputation: 29179

How About

int myInt = 88;

// Will not compile

Long myLong = myInt;

// Compiles, and retains the non-NULL spirit of int. The best cast is no cast at all. Of course, your use case may require Long and possible NULL values. But if the int, or other longs are your only input, and your method can be modified, I would suggest this approach.

long myLong = myInt;

// Compiles, is the most efficient way, and makes it clear that the source value, is and will never be NULL.

Long myLong = (long) myInt;

Upvotes: 8

Mohammadreza Tavakoli
Mohammadreza Tavakoli

Reputation: 420

use

new Long(your_integer);

or

Long.valueOf(your_integer);

Upvotes: 20

TG Gowda
TG Gowda

Reputation: 11957

We shall get the long value by using Number reference.

public static long toLong(Number number){
    return number.longValue();
}

It works for all number types, here is a test:

public static void testToLong() throws Exception {
    assertEquals(0l, toLong(0));   // an int
    assertEquals(0l, toLong((short)0)); // a short
    assertEquals(0l, toLong(0l)); // a long
    assertEquals(0l, toLong((long) 0)); // another long
    assertEquals(0l, toLong(0.0f));  // a float
    assertEquals(0l, toLong(0.0));  // a double

}

Upvotes: 3

cloudy_weather
cloudy_weather

Reputation: 2997

In Java you can do:

 int myInt=4;
 Long myLong= new Long(myInt);

in your case it would be:

content.setSequence(new Long(i));

Upvotes: 7

serg
serg

Reputation: 111365

Use the following: Long.valueOf(int);.

Upvotes: 235

Maxim Veksler
Maxim Veksler

Reputation: 30232

I have this little toy, that also deals with non generic interfaces. I'm OK with it throwing a ClassCastException if feed wrong (OK and happy)

public class TypeUtil {
    public static long castToLong(Object o) {
        Number n = (Number) o;
        return n.longValue();
    }
}

Upvotes: 6

MaskedCoder
MaskedCoder

Reputation: 29

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.

Upvotes: 2

saret
saret

Reputation: 2227

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

Integer y = 1;
long x = y.longValue();

Upvotes: 27

Daniel Earwicker
Daniel Earwicker

Reputation: 116744

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.

Upvotes: 268

Related Questions