1、Java设计的基本原则:一切皆对象,一切操作都以对象的形式进行描述。此时我们发现,有一个矛盾点,基本数据类型不是对象。对于这个矛盾点,我们可以进行处理:
class MyInt { // 定义类
private int num; // 类封装基本数据类型
// 定义构造方法,将基本数据类型传递给对象
public MyInt(int num) {
this.num = num;
}
public int intValue() { // 返回被封装的数据
return this.num;
}
}
public class Demo {
public static void main(String[] args) {
MyInt mi = new MyInt(10); // 装箱
int temp = mi.intValue(); // 拆箱
// 被封装的数据只有拆箱后才能进行计算
System.out.println(temp * 2);
}
}
Java为了方便用户使用,专门提供了一组包装类,来包装所有的基本类型:byte(Byte)
、short(Short)
、int(Integer)
、long(Long)
、float(Float)
、double(Double)
、boolean(Boolean)
、char(Character)
。()中为基本类型对应的包装类。
以上包装类严格讲分为两种子类型:
· 对象型包装类(Object直接子类):Character,Boolean;
· 数值型包装类(Number直接子类):Byte、Short、Integer、Float、Long、Double
Number
是一个抽象类,一共定义了六个操作方法:byteValue()
、shortValue()
、integerValue()
、floatValue()
、longValue()
、doubleValue()
。
装箱与拆箱
1、基本数据类型与包装类间的转换通过以下方式:
· 装箱操作: 将基本数据类型变为包装类的形式:
|- 每个包装类的构造方法都可以接收各自数据类型的变量;
· 拆箱操作: 将包装类取出被包装基本数据类型数据:
|- 利用Number类中提供的一些列xxValue()方法
范例:使用int和Integer
public class Demo {
public static void main(String[] args) {
Integer obj = new Integer(10); // 装箱
int temp = obj.intValue(); // 拆箱
System.out.println(temp * 2);
}
}
范例:使用double和Double
public class Demo {
public static void main(String[] args) {
Double obj = new Double(10.2); // 装箱
double temp = obj.doubleValue(); // 拆箱
System.out.println(temp * 2);
}
}
范例:使用boolean和Boolean
public class Demo {
public static void main(String[] args) {
Boolean obj = new Boolean(true); // 装箱
boolean temp = obj.booleanValue(); // 拆箱
System.out.println(!temp);
}
}
2、所有包装类都利用同种格式的方法进行操作。但在JDK1.5之后,增加了自动装箱和自动拆箱的机制,并且可以直接使用包装类的对象进行数字计算,为开发提供了便利。
public class Demo {
public static void main(String[] args) {
Integer obj = 10; // 自动装箱
int temp = obj; // 自动拆箱
obj++; // 包装类直接进行数学计算
System.out.println(temp * obj);
}
}
3、注意点:Integer对象可以直接赋值,也可以使用构造方法赋值,二者有何区别?
两者的区别可以参考String类直接赋值和构造方法赋值的区别。
public class Demo {
public static void main(String[] args) {
Integer objA = 10; // 自动装箱
Integer objB = 10; // 自动装箱
Integer objC = new Integer(10);
System.out.println(objA == objB); // true
System.out.println(objA == objC); // false
System.out.println(objA.equals(objC)); // true
}
}
使用包装类时,几乎都是直接赋值,很少使用构造方法,判断内容是否相等时依然使用equals()
方法。
4、Object类可以接收一切引用数据类型,由于自动装箱机制的存在,Object也可以接收基本数据类型。
流程:基本数据类型 → 自动装箱(成为对象) → 向上转型为Object。
public class Demo {
public static void main(String[] args) {
Object obj = 10;
// Object不能直接向下转型向int
// Object只能自动转型为Integer
int temp = (Integer) obj;
System.out.println(temp * 2);
}
}
数据类型转换
1、包装类多用于数据类型转换:在包装类中提供了将String数据变为基本数据类型的方法。
如下:
· Integer类:public static int parseInt(String s);
· Double类:public static double parseDouble(String s);
· Boolean类:public static boolean parseBoolean(String s);
注意:Character
类不存在将字符串变为字符的方法,但String
类的charAt()
可以根据索引取出字符。
范例:将String变为int型
public class Demo {
public static void main(String[] args) {
String str = "123";
int temp = Integer.parseInt(str);
System.out.println(temp * 2);
}
}
将字符串变为int型的前提是:该字符串全部为数字.
范例:将String变为double型
public class Demo {
public static void main(String[] args) {
String str = "12.3";
double temp = Double.parseDouble(str);
System.out.println(temp * 2);
}
}
范例:将String变为boolean
public class Demo {
public static void main(String[] args) {
String str = "true";
boolean temp = Boolean.parseBoolean(str);
System.out.println(!temp);
}
}
Boolean转换时,如果字符串的值不是true或false,默认转为false。
2、字符串可以变为基本数据类型,基本数据类型也可变为字符串。方法有两种:
(1)任何基本数据类型与字符串使用+
连接后,都会变为String类型数据
public class Demo {
public static void main(String[] args) {
int num = 100;
String str = num + ""; // 变为String类
System.out.println(str.replaceAll("0", "9")); // 199
}
}
上述方式虽然完成了转换,但产生了垃圾""
(2)利用public static String valueOf(数据类型 变量)
public class Demo {
public static void main(String[] args) {
int num = 100;
String str = String.valueOf(num);
System.out.println(str.replaceAll("0", "9")); // 199
}
}
上述方式不会产生垃圾,因此开发中常用上述方法。