8種基本類型的包裝類和常量池簡單介紹

/**
 * 8種基本類型的包裝類和對象池
 * 		包裝類:java提供的爲原始數據類型的封裝類,如:int(基本數據類型),Integer封裝類。
 * 		對象池:爲了一定程度上減少頻繁創建對象,將一些對象保存到一個"容器"中。
 * 
 * 	Byte,Short,Integer,Long,Character。這5種整型的包裝類的對象池範圍在-128~127之間,也就是說,
 * 	超出這個範圍的對象都會開闢自己的堆內存。
 * 
 *  Boolean也實現了對象池技術。Double,Float兩種浮點數類型的包裝類則沒有實現。
 * 	String也實現了常量池技術。
 * 
 * 自動裝箱拆箱技術
 * 	JDK5.0及之後允許直接將基本數據類型的數據直接賦值給其對應地包裝類。
 *  如:Integer i = 3;(這就是自動裝箱)
 *  實際編譯代碼是:Integer i=Integer.valueOf(3); 編譯器自動轉換
 * 	自動拆箱則與裝箱相反:int i = (Integer)5;
 */

public class Test {
	public static void main(String[] args) {
		
		//基本數據類型常量池範圍-128~127
		Integer n1 = -129;
		Integer n2 = -129;
		Long n3 = 100L;
		Long n4 = 100L;
		Double n5 =  12.0;
		Double n6 = 12.0;
		//false
		System.out.println(n1 == n2);
		//true
		System.out.println(n3 == n4);
		//false
		System.out.println(n5 == n6);
		
		//String常量池技術,注意:這裏String不是用new創建的對象
		String str1 = "abcd";
		String str2 = "abcd";
		//true
		System.out.println(str1 == str2);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章