java type conversion valueOf and parse...

valueOfThe return is a wrapper class, the parameter can be String or this class
parse...returns a basic type, and the parameter can only be String

When performing type conversion:
If you want to get the basic type, use parse...
it If you want to get the packaging type, usevalueOf

In addition, when obtaining the packaging type from the basic type, try to use valueOfit instead of newobtaining the packaging type, because valueOfthe cache is used.

    public static Integer valueOf(int i) {
    
    
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

Reference:
https://blog.csdn.net/xiong9999/article/details/87800317

Guess you like

Origin blog.csdn.net/claroja/article/details/114198876