前言
提示:这里可以添加本文要记录的大概内容:
上一篇写了一些关于 String类的源码分析,这次再记录下 Integer包装!类😜😜😜
提示:以下是本篇文章正文内容,下面案例可供参考
一、源码解析
Integer实现接口:
public final class Integer extends Number implements Comparable<Integer> {
- final 修饰,不可被继承,继承Number类实现Comparable接口。
变量:
// 使用了int类型的value来存储值
private final int value;
// Integer的最大值为2^31-1
public static final int MIN_VALUE = 0x80000000;//最大值
// 最小值为-2^31
public static final int MAX_VALUE = 0x7fffffff;//最小值
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
构造函数:
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
// 输出一个十进制数。
this.value = parseInt(s, 10);
}
常用方法:
- parseInt
- valueOf
- equals
- hashcode
- toString
二、方法介绍
parseInt
代码如下(示例):
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");
}
//判断基数是否在 2~36之间
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix "