Java學習筆記24(Integer類)

來源:互聯網
上載者:User

標籤:方便   fun   equals   static   比較   自動   port   基礎資料型別 (Elementary Data Type)   bin   

這裡介紹基礎資料型別 (Elementary Data Type)封裝類,Integer是int的封裝類,

其他的基礎資料型別 (Elementary Data Type)的封裝類的方法和Integer的方法幾乎一致,會一種即可全會

基礎資料型別 (Elementary Data Type)封裝類的特點:用於在基礎資料型別 (Elementary Data Type)和字串之間進行轉換

這些類屬於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以後出現的特性:自動裝箱,自動拆箱

自動裝箱:基礎資料型別 (Elementary Data Type),直接變成對象

自動拆箱:對象中的資料變回基礎資料型別 (Elementary Data Type)

樣本:

 

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    }}

 

Java學習筆記24(Integer類)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.