Integer和String转换

Integer和String相互转换

// 1、String 转 Integer
int value = Integer.parseInt(s); //string转int
Integer value = Integer.valueof(s); //string 转 integer, 进行了装箱操作Integer.valueOf(parseInt(s, 10))

// 2、int 转 String
String.valueOf(value); //Integer.toString(i)
Integer.toString(value);

// 3、Integer 转 String
String.valueOf(value); //obj.toString()
value.toString(value); //通过实例对象value调用静态方法,Integer.toString(int)
Integer.toString(value); //通过类名Integer调用静态方法,Integer.toString(int)
1、String 转 Integer

Integer.parseInt(string s)

  • 对s和radix作判断 (radius是进制,默认为10进制)

  • 对符号位"+"、"-"和字符串长度作处理

  • 取出string中的每个字符转换为数字digit,不能出现非数字位(只能包含char类型 0-9)

  • result乘以radix,result 减去digit

  • 均无异常,如果是负数,则返回result;正数返回-result

// Integer.java
public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10); //默认以十进制方式进行转换
    }

public static int parseInt(String s, int radix) throws NumberFormatException {
      // s为null,抛出NumberFormatException异常
      // radix小于2,或者radix,大于36,抛出NumberFormatException异常

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // 是否是以符号位"+"、"-"开头
                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++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s); // s
        }
        return negative ? result : -result;
    }
//Character.java
 public static int digit(char ch, int radix) {
        return digit((int)ch, radix);
    }

public static int digit(int codePoint, int radix) {
        if (radix < MIN_RADIX || radix > MAX_RADIX) {
            return -1;
        }
        if (codePoint < 128) {
            // Optimized for ASCII
            int result = -1;
            if ('0' <= codePoint && codePoint <= '9') {
                result = codePoint - '0';
            } else if ('a' <= codePoint && codePoint <= 'z') {
                result = 10 + (codePoint - 'a');
            } else if ('A' <= codePoint && codePoint <= 'Z') {
                result = 10 + (codePoint - 'A');
            }
            return result < radix ? result : -1;
        }
        return digitImpl(codePoint, radix);
    }
2、Integer 转 String

无论是int类型还是integer类型,最终都是通过Integer的toString(int i)这个静态方法进行转换的
源码自行了解吧,里面还做了一次缓存。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值