测试常量池
Posted tekikesyo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试常量池相关的知识,希望对你有一定的参考价值。
那天学些了常量池的一些特性, 写了一些代码来验证理论.
1 public class TestConstantsPool { 2 public static void main(String[] args) { 3 integerPoolNewInteger(); 4 } 5 6 static void stringPool() { 7 String a = "a123"; 8 //用常量池 9 String a_1 = a.intern(); 10 //用常量池 11 String b = "a123"; 12 //不使用常量池 13 String c = new String("a123"); 14 15 boolean e = a_1 == a; 16 boolean e2 = a_1.equals(a); 17 System.out.println(a_1); 18 } 19 20 //-128~127以内的Integer值 21 static void integerPool() { 22 Integer a1 = 127; 23 Integer b1 = 127; 24 //true 25 boolean e = a1 == b1; 26 //true 27 boolean e2 = a1.equals(b1); 28 29 System.out.println(e); 30 } 31 32 33 //-128~127以内的new Integer值 34 static void integerPoolNewInteger() { 35 Integer z1 = 127; 36 37 //不使用常量池 38 Integer a1 = new Integer(127); 39 Integer b1 = new Integer(127); 40 41 //引用地址同 a1 42 Integer aa1 = a1; 43 //引用地址同 b1 44 Integer bb1 = b1; 45 //引用地址同 z1 46 Integer zz1 = z1; 47 48 //使用常量池 49 Integer c1 = Integer.valueOf("127"); 50 Integer d1 = Integer.parseInt("127"); 51 52 //false 53 boolean e = a1 == b1; 54 //true 55 boolean e2 = a1.equals(b1); 56 57 System.out.println(e); 58 } 59 60 61 //>=128 62 static void integerPool128() { 63 Integer a1 = 128; 64 Integer b1 = 128; 65 66 //false 67 boolean e = a1 == b1; 68 //true 69 boolean e2 = a1.equals(b1); 70 71 System.out.println(e); 72 } 73 74 75 static void decimalPool_1() { 76 BigDecimal a1 = BigDecimal.ONE; 77 BigDecimal a2 = BigDecimal.ONE; 78 79 //true 80 boolean e = a1 == a2; 81 //true 82 boolean e2 = a1.equals(a2); 83 84 System.out.println(e); 85 86 } 87 88 static void decimalPool_2() { 89 BigDecimal a1 = new BigDecimal(1); 90 BigDecimal a2 = BigDecimal.ONE; 91 92 //false 93 boolean e = a1 == a2; 94 //true 95 boolean e2 = a1.equals(a2); 96 97 System.out.println(e); 98 99 } 100 101 static void decimalPool_3() { 102 BigDecimal a1 = new BigDecimal(1); 103 BigDecimal a2 = new BigDecimal(1); 104 105 //false 106 boolean e = a1 == a2; 107 //true 108 boolean e2 = a1.equals(a2); 109 110 System.out.println(e); 111 112 } 113 114 //Byte,Short,Integer,Long,Character,Boolean。这5种包装类默认创建了数值[-128,127]的相应类型的缓存数据,但是超出此范围仍然会去创建新的对象 115 //浮点数类型的包装类Float,Double 116 }
public class TestConstantsPool { public static void main(String[] args) { integerPoolNewInteger(); }
static void stringPool() { String a = "a123"; //用常量池 String a_1 = a.intern(); //用常量池 String b = "a123"; //不使用常量池 String c = new String("a123");
boolean e = a_1 == a; boolean e2 = a_1.equals(a); System.out.println(a_1); }
//-128~127以内的Integer值 static void integerPool() { Integer a1 = 127; Integer b1 = 127; //true boolean e = a1 == b1; //true boolean e2 = a1.equals(b1);
System.out.println(e); }
//-128~127以内的new Integer值 static void integerPoolNewInteger() { Integer z1 = 127;
//不使用常量池 Integer a1 = new Integer(127); Integer b1 = new Integer(127);
//引用地址同 a1 Integer aa1 = a1; //引用地址同 b1 Integer bb1 = b1; //引用地址同 z1 Integer zz1 = z1;
//使用常量池 Integer c1 = Integer.valueOf("127"); Integer d1 = Integer.parseInt("127");
//false boolean e = a1 == b1; //true boolean e2 = a1.equals(b1);
System.out.println(e); }
//>=128 static void integerPool128() { Integer a1 = 128; Integer b1 = 128;
//false boolean e = a1 == b1; //true boolean e2 = a1.equals(b1);
System.out.println(e); }
static void decimalPool_1() { BigDecimal a1 = BigDecimal.ONE; BigDecimal a2 = BigDecimal.ONE;
//true boolean e = a1 == a2; //true boolean e2 = a1.equals(a2);
System.out.println(e);
}
static void decimalPool_2() { BigDecimal a1 = new BigDecimal(1); BigDecimal a2 = BigDecimal.ONE;
//false boolean e = a1 == a2; //true boolean e2 = a1.equals(a2);
System.out.println(e);
}
static void decimalPool_3() { BigDecimal a1 = new BigDecimal(1); BigDecimal a2 = new BigDecimal(1);
//false boolean e = a1 == a2; //true boolean e2 = a1.equals(a2);
System.out.println(e);
}
//Byte,Short,Integer,Long,Character,Boolean。这5种包装类默认创建了数值[-128,127]的相应类型的缓存数据,但是超出此范围仍然会去创建新的对象 //浮点数类型的包装类Float,Double}
以上是关于测试常量池的主要内容,如果未能解决你的问题,请参考以下文章