从头开始学JDK-------Integer

目录

* Integer构造

* Integer # valueOf(int i) 

* Integer # valueOf(String) : Integer

* Integer # equals

* Integer # compareTo(Integer):int

* Integer # toString():String

* Integer # toString(int,int):String 

* Integer # parseInt(String,int):int

* Integer # max、min、sum

* Integer # extends Number


* Integer构造

        Integer是基本数据类型int的包装类。内部维护了一个int 对象,需要在初始化的时候指明。

        第一个构造函数接收一个int数字,维护到内部的value中。

        第二个构造函数解析 String类型的字符串,把字符串解析为10进制的数字。抛出RuntimeException的子类异常NumberFormatException

public final class Integer ...

    private final int value;
  
    public Integer(int value) {
        this.value = value;
    }

    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }

* 衍生 Integer最大值是多少?

        Integer占4个字节,共4*8 = 32位,除去符号位共31位能存放数据,所以最大值是2^31次方 - 1。 

        至于为什么要减1,因为达到最大值就要进一位了。例如十进制中最大的两位数是 10^2 - 1。

        最小值是 -2^31次方

        int i = Integer.MAX_VALUE + 1;
        //true
        System.out.println(new Integer(i).intValue() == Integer.MIN_VALUE);

        int j = Integer.MAX_VALUE + 2;
        //true
        System.out.println(j == (Integer.MIN_VALUE + 1));

* Integer # valueOf(int i) 

        静态方法valueOf方法内部使用了缓存机制,即Integer对象内部维护了一个缓存池。

        当int数字在 [ -128 ,127]之间的时候,返回内部池子中的对象。

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

 * 衍生  自动装箱也是调用的这个静态方法

         Integer i1 = 100; 等价于 Integer i1 = Integer.valueOf(100); 

         i2是新创建的一个对象,分配的是一块新的堆内存地址。

        Integer i1 = 100;  // ==> Integer i1 = Integer.valueOf(100);  
        Integer i2 = new Integer(100);
        Integer i3 = Integer.valueOf(100);
        //false
        System.out.println(i1 == i2);
        //true
        System.out.println(i1 == i3);

* Integer # valueOf(String) : Integer

         Integer的parseInt方法能把字符串按照指定进制(默认10进制)解析成数字。

class Integer

public static Integer valueOf(String s) 
                      throws NumberFormatException{

     return Integer.valueOf( parseInt(s,10) );
}

        //true
        Integer.valueOf(100) == Integer.valueOf("100");

* Integer # equals

        比较的是两个Integer对象的内部的int value值。

class Integer ...
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }

* Integer # compareTo(Integer):int

         与另外的一个Integer比较大小,如果比另外一个大,返回1

    public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

    public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }

* Integer # toString():String

        把内部的value操作成字符串。

    public String toString() {
        return toString(value);
    }

    public static String toString(int i) {
        ... 
        // char [] buf
        return new String(buf, true);
    }

* Integer # toString(int,int):String 

         第一个参数是int数字,第二个参数是几进制。

* Integer # parseInt(String,int):int

          按照指定进制,解析字符串,返回int数字。第一个参数为null会报错。

    // java.lang.NumberFormatException: null
    Integer.max(1,new Integer(null));

    public Integer(String s) throws NumberFormatException {
        this.value = parseInt(s, 10);
    }

* Integer # max、min、sum

        数字函数,见名知意。别忘了用。

* Integer # extends Number

public abstract class Number ... 
    
    public abstract int intValue();

    public abstract long longValue();

    public abstract float floatValue();

    public abstract double doubleValue();
}


class Integer extends Number ...

    private final int value;

    public int intValue() {
        return value;
    }

    public long longValue() {
        return (long)value;
    }

    public double doubleValue() {
        return (double)value;
    }

    public float floatValue() {
        return (float)value;
    }

 

posted @ 2022-07-17 12:13  小大宇  阅读(1)  评论(0编辑  收藏  举报