Converting Integer to Long

前端 未结 16 2039
轮回少年
轮回少年 2020-12-07 13:47

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

相关标签:
16条回答
  • This is null-safe

    Number tmp = getValueByReflection(inv.var1(), classUnderTest, runtimeInstance);
    Long value1 = tmp == null ? null : tmp.longValue();
    
    0 讨论(0)
  • 2020-12-07 14:24

    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);
    
    0 讨论(0)
  • 2020-12-07 14:32
    new Long(Integer.longValue());
    

    or

    new Long(Integer.toString());
    
    0 讨论(0)
  • 2020-12-07 14:32

    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();
    
    0 讨论(0)
提交回复
热议问题