Java中的Integer类

Integer
Integer与int比较

一、构造函数

Integer(int value) 
          构造一个新分配的 Integer 对象,它表示指定的 int 值。 
Integer(String s) 
          构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。 

二、方法

int compareTo(Integer anotherInteger)
在数字上比较两个 Integer 对象。
boolean equals(Object obj)
比较此对象与指定对象。


double doubleValue()
以 double 类型返回该 Integer 的值。

float floatValue()
以 float 类型返回该 Integer 的值。

int intValue() 
          以 int 类型返回该 Integer 的值。

long longValue()
以 long 类型返回该 Integer 的值。


static int parseInt(String s) 
          将字符串参数作为有符号的十进制整数进行解析

static int parseInt(String s, int radix)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数。


static String toBinaryString(int i)
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
static String toHexString(int i)
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
static String toOctalString(int i)
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。

String toString() 
返回一个表示该 Integer 值的 String 对象。 
static String toString(int i) 
返回一个表示指定整数的 String 对象。

static String toString(int i, int radix)
返回用第二个参数指定基数表示的第一个参数的字符串表示形式。


static Integer valueOf(int i)
返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s)
返回保存指定的 String 的值的 Integer 对象。
static Integer valueOf(String s, int radix)
返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值。


针对-128~127之间的数据,做了一个数据缓冲池,如果在该范围内,每次直接赋值给Integer并不会创建新的空间!

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf


Integer a2 = 30; //自动装箱:Integer.valueOf(30)
Integer b2 = 30;
Integer c2 = Integer.valueOf(30);
Integer d2 = new Integer(30); //堆
System.out.println((a2 == b2)); //true
System.out.println((a2 == c2)); //true
System.out.println((a2 == d2)); //false
System.out.println(a2.equals(d2)); //true

由自动装箱和拆箱引发我看Integer源码
static int MAX_VALUE
值为 231-1 的常量,它表示 int 类型能够表示的最大值。
static int MIN_VALUE
值为 -231 的常量,它表示 int 类型能够表示的最小值。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值