Java基礎——基本類型封裝類型的引入(1)

來源:互聯網
上載者:User

標籤:相關   oat   als   一些事   通過   方法   cte   val   關係   


基本類型封裝類型的引入

1.概述
基本類型就是我們之前所說的類似int、String、float等常用的一些資料類型,這一些是基本的資料類型,為了對基本類型資料進行更多的操作
,以及更方便的操作,Java針對每一種資料類型提供了相應的類類型,即封裝類型。


2.對應關係

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


3.詳解
(1).Integer類的構造方法
public Integer(int value)
public Integer(String str)


(2).Integer的成員方法
String-->int
String.valueOf(number);

int--->String
Integer.parseInt(str)


(3).Integer的進位轉化
public static String toBinaryString(int i);//十進位轉二級制
public static String toOctalString(int i);//十進位轉八級制
public static String toHexString(int i);//十進位轉十二級制


(4).JDK5的自動拆裝箱
自動裝箱:把基本類型轉化為封裝類型
自動拆箱:把封裝類型轉化為基本類型
在自動拆裝箱的時候需要注意null 指標的情況。


代碼舉例:
public class Test {
public static void main(String[] args) {
int i=10;
String str="100";

//Integer的構造方法測試
Integer tg =new Integer(i);
System.out.println("test is:"+tg);
Integer tg1 =new Integer(str);
System.out.println("test is:"+str+"---"+tg1);

System.out.println("-----------------------");
//int類型和String類型之間的相互轉化
//String--->int
int getValue=Integer.parseInt(str);
System.out.println("test1 is:"+getValue);
//int--->string
String getStr=i+"";
String getStr1=String.valueOf(i);
System.out.println("test1 is:"+getStr+"---"+getStr1);
System.out.println("-----------------------");

//進位轉化測試
System.out.println("test2 10-->2:"+Integer.toBinaryString(i));
System.out.println("test2 10-->8:"+Integer.toOctalString(i));
System.out.println("test2 10-->12:"+Integer.toHexString(i));
System.out.println("-----------------------");

//自動拆裝測試
Integer t=100;
t+=20;
System.out.println("i:"+t);
//源碼
/*Integer t=Integer.valueOf(100);
t=Integer.valueOf(t.intValue()+20);*/

}
}
//輸出結果:
test is:10
test is:100---100
-----------------------
test1 is:100
test1 is:10---10
-----------------------
test2 10-->2:1010
test2 10-->8:12
test2 10-->12:a
-----------------------
i:120

 


(5).Integer的相關面試題
A:Integer i=1;i+=1;做了哪一些事情

解:Integer i=1:基本類型到參考型別,屬於自動裝箱
i+=1;參考型別到基本類型,先自動拆箱,然後又自動裝箱


B:看程式寫結果:
public class Test {
public static void main(String[] args) {
Integer i1=new Integer(127);
Integer i2=new Integer(127);
System.out.println(i1==i2);
System.out.println(i1.equals(i2));
System.out.println("-----------");

Integer i3=new Integer(127);
Integer i4=new Integer(127);
System.out.println(i3==i4);
System.out.println(i3.equals(i4));
System.out.println("************************************8");

Integer i5=128;
Integer i6=128;
System.out.println(i5==i6);
System.out.println(i5.equals(i6));
System.out.println("-----------");

Integer i7=127;
Integer i8=127;
System.out.println(i7==i8);
System.out.println(i7.equals(i8));
System.out.println("-----------");

}
}
//輸出結果:
false
true
-----------
false
true
************************************
false
true
-----------
true
true
-----------

分析:
a:由於equals比較的是存在在裡面的值,所以都是true
b:通過查源碼我們知道,征服你-128~127之間的資料,做了一個資料緩衝池,如果是該範圍以內的資料,調用時候不會建立對象。
如果超過就會建立對象。

 

Java基礎——基本類型封裝類型的引入(1)

聯繫我們

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