在Java中有八种
基本数据类型,它们只能表示简单的数据,但是不能包含一些操作数据的方法。也没有办法存储描述这些数据的内容,所以需要用一个引用数据类型,把基本数据类型进行包装
,提供一些操作基本数据类型的方法,定义描述的数据。
基本数据类型
对应的引用数据类型
就叫做基本数据类型的包装类
。
一、基本数据类型的包装类:
整数类型:
1. byte 的包装类型 Byte
内存中占 1字节 8位 so
取值范围-128到127
2. short 的包装类型 Short
内存中占 2字节 16位 so
取值范围-2^15 到2^15-1
3. int 的包装类型 Integer
内存中占 4字节 32位 so
取值范围-2^31 到2^31-1
4. long 的包装类型 Long
内存中占 8字节 64位 so
取值范围-2^63 到2^63-1
小数类型:
5. float 的包装类型 Float 占 4个字节
6. double 的包装类型 Double 占 8个字节
字符类型:
7. char Charager 2个字节 取值范围0到65535
布尔类型:
8. boolean Boolean 1个字节 1位不足8位自动填充
这里重点介绍一下Integer
,这些包装类的用法有很多雷同之处,方法也大致相同很好理解。
二、Integer
1、首先Integer类对象中,维护了一个int类型成员变量,用于表示Integer想要表示的数字
2、提供了int、Integer、String类型之间相互转换的方法 、提供了一些常量
1、Integer类的构造方法
1、Integer(int i)
:将基本数据类型的 i 转为Integer类型的对象
2、Integer(String i)
:将一个字符串类型的数字,转换成Integer类型的对象
2、Integer的成员方法
XXXXValue:可以将Integer类型的对象,转为其他的基本数据类型
(1) 例如:byteValue floatValue
3、Integer静态方法:
(1) paserInt(String str)
: 将str以十进制的方式解读为一个int值
(2) paserInt(String str,int radix)
:radix进制字符串表示的数据,转为一个int值
(3) toBinaryString(int i)
: 将i转为二进制
(4) toOctalString(int i)
: 将i转为八进制
(5) toHexString(int i)
: 将i转为十六进制
(6) toString(int i,int radix)
:将 i 转为radix指定的进制返回一个字符串
(7) ValueOf(String str,int radix)
:将str以指定的进制进行解析,封装为integer
下面是代码,可以复制到ide里跑一下
public class Demo01 {public static void main(String[] args) {//静态方法//parseInt() 将字符串中的数字转为对应的Integer类型System.out.println(Integer.parseInt("123"));//parseInt(String str,int radix) 将radix进制表示的数字str 解读为对应的十进制数字System.out.println(Integer.parseInt("101", 8));//使用2进制表示指定的数字System.out.println(Integer.toBinaryString(101));//使用八进制表示指定的数字System.out.println(Integer.toOctalString(101));//使用十六进制表示指定的数字System.out.println(Integer.toHexString(101));//toString(String str,int radix):使用指定的radix表示数字iSystem.out.println(Integer.toString(101,8));//ValueOf(String str,int radix): 参数str按照radix进制转为十进制System.out.println(Integer.valueOf("101",8));}private static void test() {//将10常量转为Integer类型Integer i = new Integer(10);//将字符串的123转为数字的123Integer i2 = new Integer("123");System.out.println(i);System.out.println(i2);//成员方法 XXXXValue将Integer类型转为其他基本数据类型float v = i.floatValue();double v1 = i.doubleValue();int i1 = i.intValue();}
}
4、Integer类型的常量
(1)、MAX_VALUE
:int类型的最大值
(2)、MIN_VALUE
: int类型的最小值
(3)、SIZE
: int类型在内存中的位数
(4)、TYPE
:int在方法区中的字节码对象
(5)、BYTES
:字节个数
public class Demo02 {public static void main(String[] args) {System.out.println(Integer.MAX_VALUE);System.out.println(Integer.MIN_VALUE);System.out.println(Integer.BYTES);System.out.println(Integer.TYPE);System.out.println(Integer.SIZE);}
}
三、自动装箱和拆箱(JDK1.5之后)
在JDK1.5之后提供了
自动装箱
和拆箱
1、装箱:将基本数据类型,封装成包装类型的对象
,这个过程就是装箱,使用构造方法即可
2、拆箱:从包装类型对象中,将基本数据类型取出
,这个过程就是拆箱,使用intValue即可
3、自动装箱和拆箱:
(1) 自动装箱:
直接将基本数据类型的数据,给对应的引用数据类型包装类型的对象直接赋值
(从基本数据类型 —> 引用数据类型)
(2) 自动拆箱:
直接使用包装类型对象,给基本数据类型的变量赋值,包装类型对应可以直接参与运算
(从引用数据类型 —> 基本数据类型)
public class Demo03 {public static void main(String[] args) {//装箱 从基本数据类型 —— —— 引用数据类型int i = 10;Integer i2 = new Integer(i);//拆箱 从引用数据类型 —— —— 基本数据类型int i3 = i2.intValue();//jdk1.5提供了自动装箱和拆箱//自动装箱Integer i4 = 10; //10是 int i4是引用//自动拆箱int i5 = i4; // i4是引用 i5是基本//还可以直接参与运算Integer i6 = i4 + i5;int i7 = i4 + i5;}
}
再说一遍哦:包装类型中,方法特点基本相同,只需要学习最典型的Integer即可