Integer类超详解
前言Integer的内部结构1、MAX_VALUE2、MIN_VALUE3、SIZE4、TYPE构造方法1、Integer(int number)2、Integer(String str)3.引用为null装、拆箱装箱拆箱隐式类型转换Integer对象的不可变性处理Integer对象的方法1、byteValue( )byte类型是什么?2、compareTo(Integer
目录(点击传送🚀🚀)
2、compareTo(Integer anotherInteger)
7、valueOf(String str[,int radix])
8、parseInt (String str[,int radix])
创作不易多多支持🧡🎈
前言
有时,需要将int这样的基础数据类型转换为对象,所有的基础数据类型都有一个与之对应的类,称为包装类(或者包装器),这些包装类的有其名字如下:
基本数据类型 | 包装类 |
int | Integer |
short | Short |
long | Long |
byte | Byte |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
包装类同String类一样,是不可变的,一旦构造了包装类对象 ,就不能改变对应包装类中的属性。同时包装类是被final修饰的类,因此不能用来派生子类(不能被继承)。
java.lang包中的Integer类在对象中包装了一个基本数据类型,它包含一个int类型的字段。此外,Integer类中还包含了很多方法,用来处理int类型的数据,例如将int和String类型之间相互转化的方法等。
不只是int类型,long,short类型都可以封装成一个类,这些类都是Number的子类。
Integer的内部结构
Integer类型里面提供了一下四个常量
1、MAX_VALUE
表示int类型可取的最大值,即
2、MIN_VALUE
表示int类型可取的最小值,即
最大值,对应的十进制,八进制,和二进制,十六进制
最小值,对应的十进制,八进制,和二进制,十六进制
其使用如下:
public class Test {
public static void main(String[] args) {
int a = Integer.MAX_VALUE;
int b = Integer.MIN_VALUE;
System.out.println("the value of Integer.MAX_VALUE is: " + a);
System.out.println("the value of Integer.MIN_VALUE is: " + b);
}
}
打印结果为:
3、SIZE
SIZE用来以二进制补码的形式表示int值的位数(int包装类位数为32,short包装类的SIZE为16)
例如:
public class Test {
public static void main(String[] args) {
int SIZEOfInteger = Integer.SIZE;
System.out.println("the value of Integer.SIZE is: " + SIZEOfInteger);
}
}
4、TYPE
表示基本数据类型int的Class实例,可以通过TYPE来验证Integer类的常量
构造方法
Integer中有一个常量value,是被final修饰的类型,也就是在构造函数对其进行初始化之后就无法修改的常量。
1、Integer(int number)
语法如下:
Integer a = new Integer( int number )
将一个int类型的数据作为参数传入其中来构建一个Integer对象
2、Integer(String str)
Integer a = new Integer( String str )
将一个String对象作为参数传入其中, 然后调用parseInt(String s,int radix)方法,将传入的字符串作为参数,将其转化为对应的int类型的数据,然后赋值给value
注意:
对于这种传入String对象的情况,str的内容必须为数值类型的字符串,否则将会抛出NumberFormatException 异常。
3.引用为null
包装类也可以不进行初始化,将其引用设置为null。但也因此会在使用他的时候抛出空指针NullPointerException异常:
Integer tem = null;
System.out.println( 1 * tem );
装、拆箱
首先,装箱和拆箱都不是虚拟机的工作,而是编译器。编译器生成字节码,虚拟机来执行这些字节码。
装箱
对于一个列表ArrayList< x > ,如果我们想要存放int类型的数据,是不能直接写成:ArrayList<int>的,因为<>尖括号中的内容是不允许为基本数据类型的,只能用一个类,例如包装类,或者自己自定义的类等。如ArrayList<Integer>。
但是java中有一个特性能很方便的将int类型直接作为对象的形式添加到ArrayList中去:
调用:
List.add(2);
自动转化为:
List.add( Integer.valueOf(2))
Integer.valueOf(x) 返回一个value值为x的Integer对象。然后传入List中去,这种转换称为自动装箱
拆箱
反过来,也当然存在拆箱
将一个Integer对象赋值给int类型的变量的时候,会自动拆箱,也就是会经历如下过程:
执行:
int a = List.get(i);
转换为:
int a = List.get(i).intValue();
intValue()方法返回调用这个方法的Integer类的value值(也就是其对应的整形值),然后赋值给a变量。
自动装拆箱对于普通的算数表达式也适用。例如:
Integer n = 3;
n++
编译器会将其拆箱,转化为int类型的数据,然后自增,随后进行装箱,再次还原为Integer对象
隐式类型转换
如果一个表达式中同时使用了Integer对象和Double对象,那么Integer对象会拆箱,然后将里面的基础数据类型提升为double,然后在将其装箱为Double对象。例如:
public class Test {
public static void main(String[] args) {
Integer tem1 = 100;
Double x = 2.0;
System.out.println(true ? x:tem1);
}
}
输出结果为2.0;
Integer对象的不可变性
有如下代码:
public static void cal(int x){
x = x + 1;
}
我们都知道,形参是实参的一份临时拷贝,实参传进来用形参接收,然后在方法里面改变的是形参,而没有改变实参,所以传进来的实参是不会自增1的。
有些人可能会认为,能通过包装类来实现一个可以改变数值参数的方法,他会自动拆箱和装箱。但事实上是不可以的。例如:
public static void cal(Integer x){
x = x + 1;
}
因为包装类是无法修改的,Integer对象里面的value值是被final修饰的,是无法修改的。
处理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);
}
}
这是因为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));
}
}
结果为:
本章完.............
更多推荐
所有评论(0)