Converting Integer to Long

前端 未结 16 2227
轮回少年
轮回少年 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条回答
  •  甜味超标
    2020-12-07 14:21

    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.

提交回复
热议问题