Converting Integer to Long

前端 未结 16 2177
轮回少年
轮回少年 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:09

    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;
    }
    

提交回复
热议问题