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

相关文章
相关标签/搜索