Integer类【JDK源码分析】

简介: Integer类【JDK源码分析】

前言

2023-10-9 14:29:39

以下内容源自《【JDK源码分析】》

仅供学习交流使用

推荐

Integer类【JDK源码分析】

Integer类

parseInt()

测试代码

源码分析在Solution类中

其巧妙之处在于:

使用负数保存运算结果:

// Accumulating negatively avoids surprises near MAX_VALUE
负积累可避免在MAX_VALUE附近出现意外
int result=0;
result -= digit;

预先判断1

if (result < multmin) {//保证不能 <-214748364 ,否则*radix之后必然越界
  throw forInputString(s);
}
result *= radix;

预先判断2

if (result < limit + digit) {//保证不能result-digit<limit,否则-digit;之后必然越界
  throw forInputString(s);
}
result -= digit;//这里是负数保存

测试代码

package testlang;
public class TestInteger1 {
    public static void main(String[] args) {
        int max=Integer.MAX_VALUE;//2147483647
        int min=Integer.MIN_VALUE;//-2147483648
        try {
            String minS="-2147483649";
            int m = Integer.parseInt(minS);
            System.out.println(m);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        try {
            String maxS="2147483648";
            int n = Integer.parseInt(maxS);
            System.out.println(n);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
    static class Solution{
        public static int parseInt(String s) throws NumberFormatException {
            return parseInt(s,10);
        }
        public static int parseInt(String s, int radix)
                throws NumberFormatException
        {
            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");
            }
            //result是按照负数来处理结果的
            //result -= digit;
            int result = 0;
            boolean negative = false;
            int i = 0, len = s.length();
            int limit = -Integer.MAX_VALUE;//-2147483647
            int multmin;
            int digit;
            if (len > 0) {
                char firstChar = s.charAt(0);
                if (firstChar < '0') { // Possible leading "+" or "-"
                    if (firstChar == '-') {
                        negative = true;
                        limit = Integer.MIN_VALUE;//-2147483648
                    } else if (firstChar != '+')
                        throw forInputString(s);
                    if (len == 1) // Cannot have lone "+" or "-"
                        throw forInputString(s);
                    i++;
                }
                multmin = limit / radix;//-214748364
                while (i < len) {
                    // Accumulating negatively avoids surprises near MAX_VALUE
                    digit = Character.digit(s.charAt(i++),radix);
                    if (digit < 0) {
                        throw forInputString(s);
                    }
                    if (result < multmin) {//保证不能 <-214748364 ,否则*radix之后必然越界
                        throw forInputString(s);
                    }
                    result *= radix;
                    if (result < limit + digit) {//保证不能result-digit<limit,否则-digit;之后必然越界
                        throw forInputString(s);
                    }
                    result -= digit;//这里是负数保存
                }
            } else {
                throw forInputString(s);
            }
            return negative ? result : -result;
        }
        //NumberFormatException.forInputString()
        static NumberFormatException forInputString(String s) {
            return new NumberFormatException("For input string: \"" + s + "\"");
        }
    }
}

parseByte()

源码是调用parseInt()之后判断是否溢出

可以借鉴parseInt()来自己实现此代码

代码

package testlang;
public class TestByte {
    public static void main(String[] args) {
        try {
            String str="-129";
            byte b = parseByte(str);
            System.out.println(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String str="+128";
            byte b = parseByte(str);
            System.out.println(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String str="+127";
        byte b = parseByte(str);
        System.out.println(b);
        str="-128";
        b = parseByte(str);
        System.out.println(b);
    }
    public static byte parseByte(String s){
        int radix=10;
        if (s==null){
            throw new NumberFormatException("null");
        }
        byte result=0;//结果按负数保存
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Byte.MAX_VALUE;//-127
        int multmin;
        int digit;
        if (len>0){
            char firstChar=s.charAt(0);
            if (firstChar<'0'){
                if (firstChar=='-'){
                    negative=true;
                    limit=Byte.MIN_VALUE;
                }else if (firstChar!='+'){//非法的符号
                    throw forInputString(s);
                }
                if (len==1){//不能只包含符号
                    throw forInputString(s);
                }
                i++;
            }
            multmin=limit/radix;
            while (i<len){
                digit=Character.digit(s.charAt(i++),radix);
                if (digit<0){
                    throw forInputString(s);
                }
                if (result<multmin){
                    throw forInputString(s);
                }
                result*=radix;
                if (result<limit+digit){
                    throw forInputString(s);
                }
                result-=digit;
            }
            return negative?result: (byte) -result;
        }else {
            throw forInputString(s);
        }
    }
    static NumberFormatException forInputString(String s) {
        return new NumberFormatException("For input string: \"" + s + "\"");
    }
}

最后

2023-10-9 14:38:43

我们都有光明的未来

祝大家考研上岸

祝大家工作顺利

祝大家得偿所愿

祝大家如愿以偿

点赞收藏关注哦

相关文章
|
3月前
|
存储 安全 Java
【JDK 源码分析】ConcurrentHashMap 底层结构
【1月更文挑战第27天】【JDK 源码分析】ConcurrentHashMap 底层结构
|
3月前
|
Java
【JDK 源码分析】HashMap 操作方法
【1月更文挑战第27天】【JDK 源码分析】HashMap Put 元素 初始化
|
4月前
|
存储 网络协议 Java
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)(二)
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)
38 0
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)(二)
|
5月前
源码分析系列教程(12) - 手写Map框架(基于JDK1.7)
源码分析系列教程(12) - 手写Map框架(基于JDK1.7)
22 0
|
2月前
|
存储 网络协议 Java
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)
15 0
|
3月前
|
安全 Java
【JDK 源码分析】HashMap 线程安全问题分析
【1月更文挑战第27天】【JDK 源码分析】HashMap 线程安全问题分析
|
3月前
|
存储 Java
【JDK 源码分析】HashMap 底层结构
【1月更文挑战第27天】【JDK 源码分析】HashMap 底层结构
|
8月前
|
Java API 索引
LinkedList类【JDK源码分析】
LinkedList类【JDK源码分析】
35 0
|
4月前
JDK8之stream流的使用:归约类方法
JDK8之stream流的使用:归约类方法
23 0
|
4月前
|
网络协议 Java Unix
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)(一)
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)
52 0
【Java】BIO源码分析和改造(GraalVM JDK 11.0.19)(一)