Converting Integer to Long

JavaReflectionCasting

Java Problem Overview


I need to get the value of a field using reflection. It so happens that I am not always sure what the datatype of the field is. For that, and to avoid some code duplication I have created the following method:

@SuppressWarnings("unchecked")
private static <T> T getValueByReflection(VarInfo var, Class<?> classUnderTest, Object runtimeInstance) throws Throwable {
  Field f = classUnderTest.getDeclaredField(processFieldName(var));
  f.setAccessible(true);
  T value = (T) f.get(runtimeInstance);

  return value;
}

And use this method like:

Long value1 = getValueByReflection(inv.var1(), classUnderTest, runtimeInstance);

or

Double[] value2 = getValueByReflection(inv.var2(), classUnderTest, runtimeInstance);

The problem is that I can't seem to cast Integer to Long:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

Is there a better way to achieve this?

I am using Java 1.6.

Java Solutions


Solution 1 - Java

Simply:

Integer i = 7;
Long l = new Long(i);

Solution 2 - Java

No, you can't cast Integer to Long, even though you can convert from int to long. For an individual value which is known to be a number and you want to get the long value, you could use:

Number tmp = getValueByReflection(inv.var1(), classUnderTest, runtimeInstance);
Long value1 = tmp.longValue();

For arrays, it will be trickier...

Solution 3 - Java

Integer i = 5; //example

Long l = Long.valueOf(i.longValue());

This avoids the performance hit of converting to a String. The longValue() method in Integer is just a cast of the int value. The Long.valueOf() method gives the vm a chance to use a cached value.

Solution 4 - Java

Oddly enough I found that if you parse from a string it works.

 int i = 0;
 Long l = Long.parseLong(String.valueOf(i));
 int back = Integer.parseInt(String.valueOf(l));

Win.

Solution 5 - Java

If the Integer is not null

Integer i;
Long long = Long.valueOf(i);

i will be automatically typecast to a long.

Using valueOf instead of new allows caching of this value (if its small) by the compiler or JVM , resulting in faster code.

Solution 6 - Java

Converting Integer to Long Very Simple and many ways to converting that
Example 1

 new Long(your_integer);

Example 2

Long.valueOf(your_integer); 

Example 3

Long a = 12345L;

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

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

Solution 7 - Java

Convert an integer directly to long by adding 'L' to the end of Integer.

Long i = 1234L;

Solution 8 - Java

((Number) intOrLongOrSomewhat).longValue()

Solution 9 - Java

If you know that the Integer is not NULL, you can simply do this:

Integer intVal = 1;
Long longVal = (long) (int) intVal

Solution 10 - Java

new Long(Integer.longValue());

or

new Long(Integer.toString());

Solution 11 - Java

A parser from int variables to the long type is included in the Integer class. Here is an example:

int n=10;
long n_long=Integer.toUnsignedLong(n);

You can easily use this in-built function to create a method that parses from int to long:

	public static long toLong(int i){
	long l;
	if (i<0){
		l=-Integer.toUnsignedLong(Math.abs(i));
	}
	else{
		l=Integer.toUnsignedLong(i);
	}
	return l;
}

Solution 12 - Java

If you don't know the exact class of your number (Integer, Long, Double, whatever), you can cast to Number and get your long value from it:

Object num = new Integer(6);
Long longValue = ((Number) num).longValue();

Solution 13 - Java

For a nullable wrapper instance,

Integer i;
Long l = Optional.ofNullable(i)
                 .map(Long::valueOf)
                 .orElse(null);

Solution 14 - Java

This is null-safe

Number tmp = getValueByReflection(inv.var1(), classUnderTest, runtimeInstance);
Long value1 = tmp == null ? null : tmp.longValue();

Solution 15 - Java

To convert Integer into Long simply cast the Integer value

Integer intValue = 23;
Long longValue = (long) intValue;

Solution 16 - Java

In case of a List of type Long, Adding L to end of each Integer value

List<Long> list = new ArrayList<Long>();
list  = Arrays.asList(1L, 2L, 3L, 4L);

Solution 17 - Java

Try to convertValue by Jackson

ObjectMapper mapper = new ObjectMapper()
Integer a = 1;
Long b = mapper.convertValue(a, Long.class)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTiago VelosoView Question on Stackoverflow
Solution 1 - Javavahid khView Answer on Stackoverflow
Solution 2 - JavaJon SkeetView Answer on Stackoverflow
Solution 3 - JavaRich MacDonaldView Answer on Stackoverflow
Solution 4 - JavaAnonView Answer on Stackoverflow
Solution 5 - JavalostintranslationView Answer on Stackoverflow
Solution 6 - JavaNaresh KumarView Answer on Stackoverflow
Solution 7 - JavaJeff JohnyView Answer on Stackoverflow
Solution 8 - Javamax.kuzmentsovView Answer on Stackoverflow
Solution 9 - JavaSteven SpunginView Answer on Stackoverflow
Solution 10 - JavaPavlo ZvarychView Answer on Stackoverflow
Solution 11 - JavaBremsstrahlungView Answer on Stackoverflow
Solution 12 - JavakaiserView Answer on Stackoverflow
Solution 13 - JavaJin KwonView Answer on Stackoverflow
Solution 14 - JavaadkissonView Answer on Stackoverflow
Solution 15 - JavaShedrackView Answer on Stackoverflow
Solution 16 - JavaJoydeep DuttaView Answer on Stackoverflow
Solution 17 - Javalemonzone2010View Answer on Stackoverflow