Java面向物件(六)
目錄- Java面向物件(六)
- 十九、包裝類
- 19.1 八種基本型別包裝類
- 19.2 基本型別、包裝類與 String 類間的轉換,
- 19.3 基本資料型別轉換為包裝類(裝箱)
- 19.4 包裝類轉換為基本資料型別(拆箱)
- 19.5 自動裝箱拆箱
- 19.6 基本資料型別、包裝類轉換為String型別
- 19.7 String型別轉換為基本資料型別、包裝類
- 19.8 特殊例子
- 十九、包裝類
十九、包裝類
19.1 八種基本型別包裝類
java 提供了8種基本資料型別對應的包裝類,使得基本資料型別的變數具有類的特征,

19.2 基本型別、包裝類與 String 類間的轉換,

19.3 基本資料型別轉換為包裝類(裝箱)
public void test1(){
public static void main(String[] args){
int num1 = 10;
// System.out.println(num1.toString()); 報錯
Integer in1 = new Integer(num1); //通過構造器裝箱
System.out.println(in1.toString());
Integer in2 = new Integer("123"); //通過字串引數裝箱
System.out.println(in2.toString());
//報例外
// Integer in3 = new Integer("123abc");
// System.out.println(in3.toString());
Float f1 = new Float(12.3f); //通過構造器裝箱
Float f2 = new Float("12.3"); //通過字串引數裝箱
System.out.println(f1);
System.out.println(f2);
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("TrUe");
System.out.println(b2); //true
/* b2 回傳 true 原因:
public Boolean(String s) {
this(parseBoolean(s));
}
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
equalsIgnoreCase() 方法用于將字串與指定的物件比較,不考慮大小寫,
如果給定物件與字串相等,則回傳 true,否則回傳 false,
*/
Boolean b3 = new Boolean("true123");
System.out.println(b3); //false
Order order = new Order();
System.out.println(order.isMale); //false(基本資料型別默認值)
System.out.println(order.isFemale); //null(參考資料型別默認值)
}
}
class Order{
boolean isMale;
Boolean isFemale;
}
19.4 包裝類轉換為基本資料型別(拆箱)
public void test2(){
public static void main(String[] args){
Integer in1 = new Integer(12);
int i1 = in1.intValue(); //呼叫類方法xxxValue()
System.out.println(i1 + 1); // 13
Float f1 = new Float(12.3);
float f2 = f1.floatValue(); //呼叫類方法xxxValue()
System.out.println(f2 + 1); // 13.3
}
}
19.5 自動裝箱拆箱
// JDK 5.0 新特性:自動裝箱 與自動拆箱
public void test3(){
int num1 = 10;
Integer in1 = num1; //自動裝箱
boolean b1 = true;
Boolean b2 = b1; //自動裝箱
System.out.println(in1.toString());
int num3 = in1; //自動拆箱
}
19.6 基本資料型別、包裝類轉換為String型別
// String型別:呼叫String多載的valueOf(Xxx xxx)
public void test4(){
public static void main(String[] args){
int num1 = 10;
//方式1:連接運算
String str1 = num1 + "";
//方式2:呼叫String的valueOf(Xxx xxx)
float f1 = 12.3f;
String str2 = String.valueOf(f1);
Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str2); //"12.3"
System.out.println(str3); //"12.4"
}
}
19.7 String型別轉換為基本資料型別、包裝類
// 呼叫包裝類的parseXxx(String s)
public void test5(){
public static void main(String[] args){
String str1 = "123";
//錯誤的情況:
// int num1 = (int)str1;
// Integer in1 = (Integer)str1;
int num2 = Integer.parseInt(str1);
//當字串中不是純數字時,可能會報NumberFormatException
System.out.println(num2 + 1);
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1); //false
}
}
19.8 特殊例子
public void test6(){
public static void main(String[] args){
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);
// 輸出 1.0,三元運算子要求兩邊運算式資料型別一致,左邊 Integer 型別自動型別轉換為 Double
}
}
public void method1() {
public static void main(String[] args){
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j); //false
Integer m = 1;
Integer n = 1;
System.out.println(m == n); //true
Integer x = 128; //相當于new了一個Integer物件
Integer y = 128; //相當于new了一個Integer物件
System.out.println(x == y); //false
}
}
/*
Integer內部定義了IntegerCache結構,IntegerCache中定義了Integer[],保存了從-128~127范圍的整數,如果我們使用自動裝箱的方式,給Integer賦值的范圍在-128~127范圍內時,可以直接使用陣列中的元素,不用再去new了,
目的:提高效率
詳細可以看 Integer 原始碼
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499314.html
標籤:Java
上一篇:Java學習-第一部分-第一階段-第六節:面向物件編程(基礎)
下一篇:Nginx-基礎篇
