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
}
}
上述方式不會產生垃圾,因此開發中常用上述方法。