包裝類

基本數(shù)據(jù)類型對象包裝類。
為了方便操作基本數(shù)據(jù)類型值,將其封裝成了對象,在對象中定義了屬性和行為豐富了該數(shù)據(jù)的操作。
用于描述該對象的類就稱為基本數(shù)據(jù)類型對象包裝類。

byte ---- Byte
short --- Short
int --------Integer
long ------Long
float ------Float
double ---Double
char ------Character
boolean --Boolean

該包裝對象主要用基本類型和字符串之間的轉(zhuǎn)換。

基本類型--->字符串
1,基本類型數(shù)值+""
2,用String類中的靜態(tài)方法valueOf(基本類型數(shù)值);
3,用Integer的方法toString();

字符串--->基本類型

  • 使用包裝類中的靜態(tài)方法 xxx parseXxx("xxx類型的字符串");*****
    int parseInt("intstring");
    long parseLong("longstring");
    boolean parseBoolean("booleanstring");
    只有Character沒有parse方法
System.out.println(Integer.parseInt("123")+1);
  • 如果字符串被Integer進行對象的封裝。
    可使用另一個非靜態(tài)的方法,intValue();
    將一個Integer對象轉(zhuǎn)成基本數(shù)據(jù)類型值。
Integer i = new Integer("123");     
System.out.println(i.intValue());

整數(shù)具備不同的進制體現(xiàn)。

  1. 十進制-->其他進制。
    toBinaryString
    toOctalString
    toHexString
十進制-->其他進制。
        System.out.println(Integer.toBinaryString(60));//二進制
        System.out.println(Integer.toOctalString(60));//八進制
        System.out.println(Integer.toHexString(60));//16進制
  1. 其他進制-->十進制。
    parseInt("string",radix)
其他進制-->十進制。
System.out.println(Integer.parseInt("3c",16));//指將16進制數(shù)3c轉(zhuǎn)換成10進制數(shù)

Integer a = new Integer("300");
Integer b = new Integer(300);
        
System.out.println(a==b);//false
        
System.out.println(a.equals(b));//true
        
System.out.println(a.compareTo(b));//0,即相等

自動裝箱和拆箱
Integer i =  4;//i = new Integer(4);自動裝箱  簡化書寫。
i = i + 6;// i = new Integer(i.intValue() + 6); //i.intValue() 自動拆箱

        Integer a = new Integer(128);
        Integer b = new Integer(128);
        
        System.out.println(a==b);//false
        System.out.println(a.equals(b));//true
        Integer x = 129;//jdk1.5以后,自動裝箱,如果裝箱的是一個字節(jié),那么該數(shù)據(jù)會被共享不會重新開辟空間。
        Integer y = 129;
        System.out.println(x==y);//false,如果是一個自己,即(-128~127)就是true
        System.out.println(x.equals(y));//true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容