Converting Integer to Long
Simply:
Integer i = 7;
Long l = new Long(i);
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...
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.
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.