Integer类超详解-2

简介: Integer类超详解

Integer类超详解-1

https://developer.aliyun.com/article/1515784


处理Integer对象的方法

1、byteValue( )

byte类型返回改Integer的值

public class Test {
    public static void main(String[] args) {
        Integer tem = new Integer("123");
        byte retByteTem = tem.byteValue();
        System.out.println("the value of retByteTem is:" + retByteTem);
    }
}


结果如下:

byte类型是什么?

byte,即字节,由8位的二进制组成。在Java中,byte类型的数据是8位带符号的二进制数。其取值范围为: [ -128 , 127 ]  ( [ (2)1000 0000 , (2)0111 1111 ] )


可以发现,如果你传入的值为128,那么其转化为的值就会变为-128,传入129,则会变成-127:

public class Test {
    public static void main(String[] args) {
        Integer tem1 = new Integer(128);
        Integer tem2 = new Integer(129);
        byte retByteTem1 = tem1.byteValue();
        byte retByteTem2 = tem2.byteValue();
        System.out.println("the value of retByteTem is:" + retByteTem1);
        System.out.println("the value of retByteTem is:" + retByteTem2);
    }
}

1d6b78218994e0ee13c1ffda2783829c_fde8d09983944b6ebbb90c21b372f75e.png

这是因为127的二进制为1111 1111,+1变成 01 0000 0000 ,由于只能存放8位二进制数,所以会自动舍去最后一个0,变成1000 0000 然后返回,也就是 - 128,然后再+1就变成-127,以此会形成一个循环效果,也就是不管你输入多少,其值都会在这个取值范围当中。  


2、compareTo(Integer  anotherInteger)

返回一个int类型的数据,在数值上(value)比较两个Integer对象,如果相等返回0,如果调用这个方法的对象小于这个anotherInteger对象,返回一个负值,否则返回一个正数。


传入时,compareTo会调用compare方法,并把两个Integer对象的value属性作为参数传入,并比较其值然后返回对应的int类型的结果。

 

 


这里不一一举例

public class Test {
    public static void main(String[] args) {
        Integer tem1 = new Integer(1);
        Integer tem2 = new Integer(2);
        int result = tem1.compareTo(tem2);
        System.out.println("the value of tem1.compareTo(tem2) is: " + result);
    }
}


3、intValue()

返回一个int类型的数据,以int类型,返回调用这个方法的Integer对象的value值

public class Test {
    public static void main(String[] args) {
        Integer tem = new Integer(1);
        int retTem = tem.intValue();
        System.out.println("the value of tem.intValue is: "+ retTem);
    }
}


4、equals(Object  obj)

返回一个boolean类型的数据,比较调用这个方法的对象和指定的对象是否相等


       equals将Object类作为参数,但是传入的是Integer类型,由于Object是所有类的父类,所以这里发生了向上转型,随后使用instanceof关键字来判断这个传入的obj类是否为Integer类的实例,如果不是,返回false,如果是则将传入的指定的obj类的对象强转为Integer类,然后调用其intValue()方法来获取其指定对象的value值,然后将其与调用这个方法的对象进行比较,返回一个boolean类型的数据。

public class Test {
    public static void main(String[] args) {
        Integer tem1 = new Integer(1);
        Integer tem2 = new Integer(1);
        boolean result1 = tem1.equals(tem2);
        System.out.println("the value of tem1.compareTo(tem2) is: " + result1);
 
        Integer tem3 = new Integer(1);
        String tem4 = new String("1");
        boolean result2 = tem3.equals(tem4);
        System.out.println("the value of tem1.compareTo(tem2) is: " + result2);
    }
}

5、shortValue()

返回一个short类型的数据,以short类型,返回调用这个方法的Integer对象的value属性

public class Test {
    public static void main(String[] args) {
        Integer tem = new Integer(123);
        int retTem = tem.shortValue();
        System.out.println("the value of tem.shortValue is: "+ retTem);
    }
}

6、toString()

返回一个String类的数据(String类型的数据不是基础数据类型,其原理是用字符数组的形式存放),toString中存在可选参数,int radix来指定表示的进制。


调用toString()方法,会自动调用其重载的方法,以Integer对象的value值作为参数,

将其转化为一个字符串类型:

public class Test {
    public static void main(String[] args) {
        Integer tem = new Integer(123);
        String ret  = tem.toString();
        System.out.println("the value of tem.shortValue is: "+ ret);
    }
}

7、valueOf(String str[,int radix])

返回一个Integer对象,将一个十进制整数表示的字符串转化为其对应int类型数据的Integer包装类。或者采用可选参数radix来指定你所输入的进制。

例如:

public class Test {
    public static void main(String[] args) {
        Integer tem = Integer.valueOf("1111",2);
        System.out.println(tem.intValue());
    }
}


其结果为  15。

8、parseInt (String str[,int radix])

       将一个字符串作为参数,解析为带符号的十进制数字,字符串中的字符必须是10进制数字,出第一个字符可能是ASCLL字符的减号外。返回一个int整数值,其中radix为可选参数,将字符串参数解析为第二个参数指定的基数(进制)中的有符号整数


parseInt("0", 10) returns 0


parseInt("473", 10) returns 473


parseInt("+42", 10) returns 42


parseInt("-0", 10) returns 0


parseInt("-FF", 16) returns -255


parseInt("1100110", 2) returns 102


parseInt("2147483647", 10) returns 2147483647


parseInt("-2147483648", 10) returns -2147483648


parseInt("2147483648", 10) throws a NumberFormatException


parseInt("99", 8) throws a NumberFormatException


parseInt("Kona", 10) throws a NumberFormatException parseInt("Kona", 27) returns 411787


Integer对象的比较                                                                                                  

       对于对象的比较,通常会想到使用 “==” 运算符,来比较两个Integer对象是否具有相同的引用地址,例如:


public class Test {
    public static void main(String[] args) {
        Integer tem1 = 100;
        Integer tem2 = 100;
        if (tem1 == tem2){
            System.out.println("tem1 == tem2");
        }else {
            System.out.println("false");
        }
    }
}

其结果为

但是又不免遇到这样的情况

将tem1和tem2的值改为200后结果却输出的是false?

       类似于String类,String类在创建对象的时候,会首先查看需要构造的字符串对象是否已经在常量池中出现,如果出现就直接引用这个已经存在的对象。而Integer类也有相似的性质,java将经常出现的值直接包装到相同的对象中,就例如上面的100或者200,但是这并不适用于所有情况。


      所以自动装箱规范要求介于-128 ~ 127之间(包含-128和127)  的short和int类型数据包装到固定的对象中去。所以上面的tem的1和2在等于100的时候判断为“相同的引用”,为200则比较不成功。


       例如有如下代码:


Integer  tem  =  128;


将会自动装箱,转化为:

Integer tem  =  valueOf ( 128 ):

valueOf()方法原码如下:


其中IntegerCache.low 和  IntegerCache.high的定义如下:

IntegerCache.low 为-128

IntegerCache.high 为 127


       可以看出来,如果目标值不在-128 ~ 127之间(包含-128和127)这个范围内,则会新建一个对象,而不是指向原来已经存在的数据(cache数组中的Integer对象)。所以使用==直接比较会得出错误的结果


       equals()方法

       如果在这个规定范围外比较可以使用equals()方法,此部分已经在上面的方法部分介绍过

public class Test {
    public static void main(String[] args) {
        Integer tem1 = 200;
        Integer tem2 = 200;
        Integer tem3 = 300;
        System.out.println("tem1 和 tem2 比较的结果为" + tem1.equals(tem2));
        System.out.println("tem2 和 tem3 比较的结果为" + tem2.equals(tem3));
    }
}


结果为:

目录
相关文章
|
3月前
|
Java API
将`List<String>`转换为`List<Long>`
将`List<String>`转换为`List<Long>`
|
4月前
|
Java 编译器
Integer类超详解-1
Integer类超详解
46 0
|
4月前
BigDecimal转String
BigDecimal转String
31 0
|
人工智能 Java 数据库
BigDecimal 转 String
BigDecimal 转 String
137 0
|
缓存 Java API
|
安全
Synchroinzed对Integer的问题
Synchroinzed对Integer的问题
113 0
Synchroinzed对Integer的问题
Zp
|
缓存 Java
Integer的比较和注意点
Integer的比较和注意点
Zp
87 0
Integer的比较和注意点
|
存储 缓存 Java
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
144 0
【每天一道面试题】Integer、 new Integer() 、int的==比较,你全都了解吗
|
Java
Java数据类型中String、Integer、int相互间的转换
Java数据类型中String、Integer、int相互间的转换
188 0