Wrapper-裝箱和拆箱

來源:互聯網
上載者:User

標籤:sel   jdk1.5   封裝   static   max   靜態   system   oct   區別   

基礎資料型別 (Elementary Data Type):棧
  byte, short,int,long,float,double,char,boolean
封裝類型: 都處在java.lang包(不需要導包)
  Byte, Short,Integer,Long,Float,Double,Character,Boolean
  Integer中常用的方法:
    parseInt(String x):將字串轉為int類型
  數實值型別常用的方法:Xxx:具體的資料類型
    parseXxx(String x):將字串x轉換為指定的數群組類型Xxx
    parseInt,parseLong,parseFlot,parseDouble...
  1.什麼是封裝類型:堆(在記憶體中的消耗比較大)
    封裝類是將基本類型封裝到一個類中,然後提供常用的屬性和方法
  2.為什麼要用封裝類型?
    a.在程式處理過程有些時候需要使用物件類型
    b.封裝類型(類):其中提供一些屬性和方法,方便操作
  JDK1.5之後Java支援自動裝箱和拆箱
  裝箱:將基礎資料型別 (Elementary Data Type)轉化為封裝類型:調用建構函式 或調用靜態valueOf方法
    a.Integer num = new Integer(10);
    b.Integer num = Integer.valueOf(10);
  拆箱:將封裝類型轉為基礎資料型別 (Elementary Data Type):對象名.xxValue():
    Integer a = Integer.valueOf(10);
    int b = Integer.intValue(a);

public class WrapperDemo {    public static void main(String[] args) {//        List list = new ArrayList();//        list.add(new Integer(1));//將基礎資料型別 (Elementary Data Type)轉換為物件類型//        list.add(new Double(3.14));//        Integer num = new Integer(10);        Integer num = Integer.valueOf(10);        int num2 = num.intValue();//將封裝類型轉換為基礎資料型別 (Elementary Data Type)        System.out.println("Integer-->max_value="+Integer.MAX_VALUE);        System.out.println("Integer-->min_value="+Integer.MIN_VALUE);        System.out.println("Integer-->SIZE="+Integer.SIZE);        System.out.println(Integer.toBinaryString(10));//將指定的數值轉化為2進位        System.out.println(Integer.toHexString(10));//將指定的數值轉化為16進位        System.out.println(Integer.toOctalString(10));//將指定的數值轉化為8進位        String str="12";        String str2="34";//        System.out.println(str+str2);//字元拼接        int  a = Integer.parseInt(str);//將字串轉換為int類型        int  b = Integer.parseInt(str2);        System.out.println(a+b);         //JDK1.5支援自動裝箱(基本類型--->封裝類型)和拆箱(封裝類型--->基本類型)操作        Double d=3.14;//自動裝箱        double d2=d;//自動拆箱    }}

==與equals的區別:
  ==比較對象的話比較是的地址的引用是否相等
  equals:比較是對象的內容是否相等
Integer num3=123;//自動裝箱
  ==>Integer num3=Integer.valueOf(123);

public class WrapperDemo2 {    public static void main(String[] args) {        Integer num1=new Integer(123);        Integer num2=new Integer(123);        System.out.println(num1==num2);//false        System.out.println(num1.equals(num2));//true        System.out.println("-----------------------");//        Integer num3=123;//Integer num3=Integer.valueOf(123);//        Integer num4=Integer.valueOf(123);        Integer num3=1234;//Integer num3=Integer.valueOf(1234);        Integer num4=Integer.valueOf(1234);        System.out.println(num3==num4);//false        System.out.println(num3.equals(num4));//true                    }}

 

Wrapper-裝箱和拆箱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.