工具类使用-数字与字符,字符串的互转
1数字-->String,char
两种方式,字符串的工具类和数字包装类都提供了方法
1.1Integer
Integer包装类中,重写了toString方法,将num转换成String类型
String str=Integer.toString(num);
底层:
//------------------Integer.java:------------------------    
/**
     * Returns a {@code String} object representing the
     * specified integer. The argument is converted to signed decimal
     * representation and returned as a string, exactly as if the
     * argument and radix 10 were given as arguments to the {@link
     * #toString(int, int)} method.
     *
     * @param   i   an integer to be converted.
     * @return  a string representation of the argument in base 10.
     */
@HotSpotIntrinsicCandidate
public static String toString(int i) {
	int size = stringSize(i);
	if (COMPACT_STRINGS) {
		byte[] buf = new byte[size];
		getChars(i, size, buf);
		return new String(buf, LATIN1);
	} else {
		byte[] buf = new byte[size * 2];
		StringUTF16.getChars(i, size, buf);
		return new String(buf, UTF16);
	}
}
1.2String
String类提供了valueOf方法
String str=String.valueOf(num);
底层:
调用Integer里面的toString方法
2 String转int
2.1Integer工具类:parseInt
Integer类肯定得提供方法,想都不用想
仔细想想这种方法应该不是用String类提供,
int num=Integer.parseInt("239487abc");//该方法会抛出异常 包上
底层:
就是字符串按位拆下字符然后Character.digit取数字,在x*10+d拼接
点击查看代码
public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */
        if (s == null) {
            throw new NumberFormatException("null");
        }
        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }
        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+') {
                    throw NumberFormatException.forInputString(s);
                }
                if (len == 1) { // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                }
                i++;
            }
            int multmin = limit / radix;
            int result = 0;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                int digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0 || result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
            return negative ? result : -result;
        } else {
            throw NumberFormatException.forInputString(s);
        }
    }
3String转Integer
3.1Integer工具类:valueOf方法
Integer num=Integer.valueOf(str);//普通成员方法.intValue()可以把Int转为int
底层:调用了上面的parseInt
    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
4.char转数字
4.1 直接ASCII码
这个就直接可以变了,毕竟char就是个数
//先做一下判断Character.isDigit(c)
int num=c-'0';
4.2 字符转字符串再转int
String.valueOf('8');
4.3 底层字符串转数字时每一位的操作:
这里没太看懂:
int digit = Character.digit(s.charAt(i++), radix);//radix在调用时设置为10
//所以我图快可以直接
int x=Character.digit('8', 10);
本文来自博客园,作者:荧惑微光,转载请注明原文链接:https://www.cnblogs.com/yinghuoweiguang/p/15950635.html
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号