這裡介紹基本數據類型包裝類,Integer是int的包裝類, 其他的基本數據類型的包裝類的方法和Integer的方法幾乎一致,會一種即可全會 基本數據類型包裝類的特點:用於在基本數據類型和字元串之間進行轉換 這些類屬於java的核心類,不需要import Integer類的方法: parseInt方 ...
這裡介紹基本數據類型包裝類,Integer是int的包裝類,
其他的基本數據類型的包裝類的方法和Integer的方法幾乎一致,會一種即可全會
基本數據類型包裝類的特點:用於在基本數據類型和字元串之間進行轉換
這些類屬於java的核心類,不需要import
Integer類的方法:
parseInt方法
示例:
將字元串變成基本類型
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); function2(); } public static void function1() { int i = Integer.parseInt("-12"); // 可以把一個字元串變成int型 System.out.println(i / 2);// -6 } public static void function2() { int i = Integer.parseInt("1010", 2); // 將一個二進位數的字元串轉成十進位int型 System.out.println(i);// 10 } }
同樣可以將基本類型變成字元串:
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); function2(); function3(); } public static void function1() { int i = 3; String string = i + ""; System.out.println(string + 1); // 這裡輸出字元串31 } public static void function2() { int i = 3; // 這裡的toString方法不是重寫父類的方法 String string = Integer.toString(i); System.out.println(string + 1); // 輸出字元串31 } public static void function3() { int i = 5; String string = Integer.toString(i, 2); System.out.println(string); // 轉成二進位數,輸出字元串101 } }
Integerl類的構造方法:
示例:
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); } public static void function1() { Integer integer = new Integer("100"); int i = integer.intValue(); // 這裡順便複習下i++和++i的區別 // System.out.println(i++);//100 System.out.println(++i);// 101 } }
其他方法:
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); function2(); } public static void function1() { // Integer類的靜態成員變數 System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); System.out.println(Integer.SIZE); // 輸出 2147483647 -2147483648 32 } public static void function2() { int i = 666; System.out.println(Integer.toBinaryString(i));// 二進位字元串的1010011010 System.out.println(Integer.toOctalString(i));// 八進位字元串的1232 System.out.println(Integer.toHexString(i));// 十六進位的29a } }
JDK1.5以後出現的特性:自動裝箱,自動拆箱
自動裝箱:基本數據類型,直接變成對象
自動拆箱:對象中的數據變回基本數據類型
示例:
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); } public static void function1() { Integer integer = 1; //這樣寫是合適的,自動裝箱 //本質上:Integer in = new Integer(1) integer = integer + 1; //自動拆箱,把引用類型拆成基本類型再做運算 //本質上:integer+1 <==> integer.intValue()+1 = 2 //再賦值給integer時候,自動裝箱 System.out.println(integer); //列印對象,但不是對象地址,而是1 } }
自動裝箱和拆箱的好處:
方便操作,簡化代碼,使基本類型和引用類型之間可以直接計算
弊端:例如Integer in = null; in = in + 1;這裡就會出現異常,必須加入相應的處理方法
關於自動裝箱和拆箱的註意事項:
這裡有一個在Java面試中坑了很多人的地方,
package demo; public class IntegerDemo { public static void main(String[] args) { function1(); function2(); function3(); } public static void function1() { Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i==j);//false //這裡比較的是兩個對象的地址,當然不同 System.out.println(i.equals(j));//true //這裡是比較對象的數據,不比較地址 } public static void function2(){ Integer a = 500; Integer b = 500; System.out.println(a==b);//false System.out.println(a.equals(b));//true } public static void function3(){ Integer a = 127; Integer b = 127; System.out.println(a==b);//true //這裡註意,大於128就是false //當數據在bytes範圍內,JVM為了節約記憶體不會創建新對象 //這裡Integer b = 127 <==> Integer b = a System.out.println(a.equals(b));//true } }