What is .intValue() in Java?

前端 未结 8 1399
暗喜
暗喜 2020-12-08 14:01

What is the difference between them?

l is an arraylist of Integer type.

version 1:

int[] a = new int[l.size()];
for (in         


        
8条回答
  •  暖寄归人
    2020-12-08 14:45

    It's just a convenience method for getting primitive value from object of Number: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Number.html

    Consider the code:

    Integer integerValue = Integer.valueOf(123);
    float floatValue = integerValue.floatValue();
    

    The last line is a convenient method to do:

    float floatValue = (float)(int)integerValue;
    

    Since any numeric type in Java can be explicitly cast to any other primitive numeric type, Number class implements all these conversions. As usual, some of them don't make much sense:

    Integer integerValue = Integer.valueOf(123);
    int intValue = integerValue.intValue();
    int intValue2 = (int)integerValue;
    int intValue3 = integerValue;
    

提交回复
热议问题