Java -----常量池

  1. String類型 的常量池java

  2. Byte,Short,Integer,Long,Character這5種整型的包裝類也只是在對應值小於等於127而且大於等於-128時纔可以使用常量池,由於他們至佔用一個字節(-128~127);緩存


public static void main(String[] args) {
			Integer s=9;
			Integer s1 = 9;
			System.out.println(s == s1);
			Integer t=new Integer(9);
			System.out.println(s == t);
			Integer s2 = 128;
			Integer s3 = 128;
			System.out.println(s2 == s3);
			Integer s4 = 127;
			Integer s5 = 127;
			System.out.println(s4 == s5);
		}

 運行結果:spa

 

true
false
false
true

  只有在使用直接賦值時,在(-128~127)內才使用緩存,使用new關鍵字 不使用緩存code

相關文章
相關標籤/搜索