包装类
5.1 概述
Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
基本类型 | 对应的包装类(位于java.lang包中) |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
5.2 Integer类
- Integer类概述
包装一个对象中的原始类型 int 的值 - Integer类构造方法及静态方法
方法名 | 说明 |
public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
public static Integer valueOf(String s) | 返回保存指定String值的 Integer 对象 |
static string tobinarystring(int i) | 得到二进制 |
static string tooctalstring(int i) | 得到八进制 |
static string toHexstring(int i) | 得到十六进制 |
static int parseInt(string s) | 将字符串类型的整数转成int类型的整数 |
- 示例代码
//public Integer(int value):根据 int 值创建 Integer 对象(过时) Integer i1 = new Integer(100); System.out.println(i1); //public Integer(String s):根据 String 值创建 Integer 对象(过时) Integer i2 = new Integer("100"); //Integer i2 = new Integer("abc"); //NumberFormatException System.out.println(i2); System.out.println("--------"); //public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例 Integer i3 = Integer.valueOf(100); System.out.println(i3); //public static Integer valueOf(String s):返回保存指定String值的Integer对象 Integer i4 = Integer.valueOf("100"); System.out.println(i4); /* public static string tobinarystring(int i) 得到二进制 public static string tooctalstring(int i) 得到八进制 public static string toHexstring(int i) 得到十六进制 public static int parseInt(string s) 将字符串类型的整数转成int类型的整数 */ //1.把整数转成二进制,十六进制 String str1 = Integer.toBinaryString(100); System.out.println(str1);//1100100 //2.把整数转成八进制 String str2 = Integer.toOctalString(100); System.out.println(str2);//144 //3.把整数转成十六进制 String str3 = Integer.toHexString(100); System.out.println(str3);//64 //4.将字符串类型的整数转成int类型的整数 //强类型语言:每种数据在java中都有各自的数据类型 //在计算的时候,如果不是同一种数据类型,是无法直接计算的。 int i = Integer.parseInt("123"); System.out.println(i); System.out.println(i + 1);//124 //细节1: //在类型转换的时候,括号中的参数只能是数字不能是其他,否则代码会报错 //细节2: //8种包装类当中,除了Character都有对应的parseXxx的方法,进行类型转换 String str = "true"; boolean b = Boolean.parseBoolean(str); System.out.println(b);
5.3 装箱与拆箱
基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:
- 装箱:从基本类型转换为对应的包装类对象。
- 拆箱:从包装类对象转换为对应的基本类型。
用Integer与 int为例:(看懂代码即可)
基本数值---->包装对象
Integer i = new Integer(4);//使用构造函数函数
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法
包装对象---->基本数值
int num = i.intValue();
5.4 自动装箱与自动拆箱
由于我们经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。例如:
Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4);
i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
//加法运算完成后,再次装箱,把基本数值转成对象。
5.5 基本类型与字符串之间的转换
基本类型转换为String
- 转换方式
- 方式一:直接在数字后加一个空字符串
- 方式二:通过String类静态方法valueOf()
- 示例代码
public class IntegerDemo { public static void main(String[] args) { //int --- String int number = 100; //方式1 String s1 = number + ""; System.out.println(s1); //方式2 //public static String valueOf(int i) String s2 = String.valueOf(number); System.out.println(s2); System.out.println("--------"); } }
String转换成基本类型
除了Character类之外,其他所有包装类都具有parseXxx静态方法可以将字符串参数转换为对应的基本类型:
public static byte parseByte(String s)
:将字符串参数转换为对应的byte基本类型。public static short parseShort(String s)
:将字符串参数转换为对应的short基本类型。public static int parseInt(String s)
:将字符串参数转换为对应的int基本类型。public static long parseLong(String s)
:将字符串参数转换为对应的long基本类型。public static float parseFloat(String s)
:将字符串参数转换为对应的float基本类型。public static double parseDouble(String s)
:将字符串参数转换为对应的double基本类型。public static boolean parseBoolean(String s)
:将字符串参数转换为对应的boolean基本类型。
代码使用(仅以Integer类的静态方法parseXxx为例)如:
- 转换方式
- 方式一:先将字符串数字转成Integer,再调用valueOf()方法
- 方式二:通过Integer静态方法parseInt()进行转换
- 示例代码
public class IntegerDemo { public static void main(String[] args) { //String --- int String s = "100"; //方式1:String --- Integer --- int Integer i = Integer.valueOf(s); //public int intValue() int x = i.intValue(); System.out.println(x); //方式2 //public static int parseInt(String s) int y = Integer.parseInt(s); System.out.println(y); } }
注意:如果字符串参数的内容无法正确转换为对应的基本类型,则会抛出
java.lang.NumberFormatException
异常。
5.6 底层原理
建议:获取Integer对象的时候不要自己new,而是采取直接赋值或者静态方法valueOf的方式
因为在实际开发中,-128~127之间的数据,用的比较多。如果每次使用都是new对象,那么太浪费内存了。
所以,提前把这个范围之内的每一个数据都创建好对象,如果要用到了不会创建新的,而是返回已经创建好的对象。
//1.利用构造方法获取Integer的对象(JDK5以前的方式) /*Integer i1 = new Integer(1); Integer i2 = new Integer("1"); System.out.println(i1); System.out.println(i2);*/ //2.利用静态方法获取Integer的对象(JDK5以前的方式) Integer i3 = Integer.valueOf(123); Integer i4 = Integer.valueOf("123"); Integer i5 = Integer.valueOf("123", 8); System.out.println(i3); System.out.println(i4); System.out.println(i5); //3.这两种方式获取对象的区别(掌握) //底层原理: //因为在实际开发中,-128~127之间的数据,用的比较多。 //如果每次使用都是new对象,那么太浪费内存了 //所以,提前把这个范围之内的每一个数据都创建好对象 //如果要用到了不会创建新的,而是返回已经创建好的对象。 Integer i6 = Integer.valueOf(127); Integer i7 = Integer.valueOf(127); System.out.println(i6 == i7);//true Integer i8 = Integer.valueOf(128); Integer i9 = Integer.valueOf(128); System.out.println(i8 == i9);//false //因为看到了new关键字,在Java中,每一次new都是创建了一个新的对象 //所以下面的两个对象都是new出来,地址值不一样。 /*Integer i10 = new Integer(127); Integer i11 = new Integer(127); System.out.println(i10 == i11); Integer i12 = new Integer(128); Integer i13 = new Integer(128); System.out.println(i12 == i13);*/