三个特殊类——包装类:
- Object类接收所有的引用数据类型,而基本数据类型就要依靠包装类实现;
- 包装类定义:包装类就是将基本数据类型封装到类中;
- 将基本数据类型封装为一个类对象的本质就是使用Object进行接收处理;
一、自定义一个包装类
class intDemo{
private int num;
public intDemo(int num){
this.num = num;
}
public int getNum(){
return this.num;
}
}
public class test3{
public static void main(String[] args) {
//将整型常量封装到类中:
intDemo value = new intDemo(55);
System.out.println(value.getNum());
}
}
二、问题引入:
在Java中有8种基本数据类型,如果每种数据类型都按照以上方式进行编写,就会引入以下问题:
-
在开发过程中,大量代码会重复;
-
在进行数学计算的时候,必须利用明确的方法将包装的数据取出后才可以进行运行;
所以Java为了方便开发,专门提供了包装类的使用,而对于包装类的使用,提供了两种类型:
(1)、对象型(Object的直接子类):- Boolean(boolean的包装类); - Character(char的包装类);
(2)数值型(Number的直接子类,存在数值转换异常NumberFormatException):
- Byte:byte的包装类;
- Double:double的包装类;
- Short:short的包装类;
- Long:long的包装类;
- Integer(int):int的包装类;
- Float:float的包装类;
三、详解Number类(数值型):
Number类存在于java.lang包,是一个抽象类,子类继承它就必须覆写abstract修饰的方法,其中有6个abstract修饰的方法(拆箱中会用到):
- public abstract int intValue() ;
- public abstract long longValue();
- public abstract short shortValue() ;
- public abstract double doubleValue() ;
- public abstract byte byteValue();
- public abstract float floatValue() ;
四、装箱与拆箱:
【装箱】:将基本数据类型变为包装类对象,利用每个包装类提供的构造方法实现装箱;
【拆箱】:将包装类中包装的基本数据类型值取出,利用的是Number类提供的6种方法(xxValue()方法)实现拆箱处理;
[以int与integer举例]:
[注意]:
在JDK1.5之后,提供了自动的装箱与拆箱操作:
public class test3{
public static void main(String[] args) {
//手工装箱:基本数据类型——>包装类
Integer i = new Integer(10);
//手工拆箱:包装类——>基本数据类型(将基本数据类型从包装类中取出),调用的是xxValue()方法:
int result1 = i.intValue();
System.out.println(result1);
//自动装箱:
Integer j = 20;
//自动拆箱:
int result2 = j + 10;
System.out.println(result2);
}
}
——输出结果:
[阿里编码规范]:
所有的相同类型的包装类对象之间值的比较,全部使用equals()方法进行比较。
[相关练习]:
public class test3{
public static void main(String[] args) {
Integer i1 = 10;
Integer i2 = new Integer(10);
Integer i3 = 10;
Integer i4 = 200;
Integer i5 = 200;
System.out.println(i1 == i2);
System.out.println(i1 == i3);
System.out.println(i4 == i5);
}
}
[总结]:
Integer num = ?
当"?"的值在[-128, 127]时,Integer会在Integer常量池中产生,会复用已有对象,比较时可以使用“==”,当然也可以使用equals()方法;
除此之外的所有数据,都会在堆上产生,并不会复用已有对象,使用equals()方法;
因此,推荐使用equals()方法。
[阿里编码规范]:使用int还是Integer?
关于基本数据类型与包装数据类型的使用标准如下:
- 【强制】所有的 POJO(简单Java类,叫做Bean,其中的组成只有get()、set()、基本属性、构造方法)类属性必须使用包装数据类型。
- 【强制】RPC 方法(RPC:远程方法调用)的返回值和参数必须使用包装数据类型。
- 【推荐】所有的局部变量使用基本数据类型(int)。
五、字符串与基本数据类型的转换:
(一)、将字符串转换为各个基本数据类型,需要包装类的支持,调用各个包装类.parseXX(String str)方法,这个方法叫类方法或者静态方法。
-
String——>int 类型(Integer类):
public static int parseInt(String s) throws NumberFormatException -
String——>double类型(Double类):
public static double parseDouble(String s) throws NumberFormatException -
String——>Boolean类型(Boolean类):
public static boolean parseBoolean(String s)
[范例1]:将字符串变为int型
public class test3{
public static void main(String[] args) {
String str = "123";
int num = Integer.parseInt(str);
System.out.println(num);
}
}
——输出结果为123。
[范例2]:将字符串变为double型
public class test3{
public static void main(String[] args) {
String str = "123";
double num = Double.parseDouble(str);
System.out.println(num);
}
}
——输出结果是123.0。
[问题引入]:
当字符串的组成中出现非数字,就会出现转换错误(NumberFormatException),如何避免呢?
——比如:
public class test3{
public static void main(String[] args) {
String str = "123a";
int num = Integer.parseInt(str);
System.out.println(num);
}
}
注意:只有数值型的包装类才存在数值转换异常!对象型不存在该问题。
(二)、将基本数据类型转换为字符串类型
——任何数据类型使用了“+”连接空白字符串就变为了字符串类型;
——使用String类中提供的valueOf()方法(静态方法),此方法不产生垃圾;
public class test3{
public static void main(String[] args) {
int num = 20;
// ①""+基本数据类型:
String str = num + "";
//证明是字符串:调用字符串才有的方法:
System.out.println(str.length());
// ②valueOf()方法:
String str1 = String.valueOf(10);
System.out.println(str1);
}
}
——输出结果: