packagesfk.bbs.test.springjsbctempletTest;importorg.junit.Test;public classtestBase
{
@Testpublic voidtest()
{/*源码
* public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}*/
//自动装箱//系统把一个[-128,127]之间的整数自动装箱成Integer实例,并放入了一个名为cache的数组中缓存起来,如果以后把一个[-128,127]之间的整数自动装箱成一个integer实例时,//实际上是直接指向对应的数组元素.因此a出的代码打印为true
Integer autoPackage0= Integer.valueOf(1);/*** public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}*/
//自动装箱,直接将值赋给一个对象时,调用就是Integer.valueOf(int i)这个方法//双等比较的是对象存在的地址
Integer autoPacking1 = 1;
Integer autoPacking2= 1;
Integer newInteger= new Integer(1);
System.out.println("在integer的cache中时");//equeals 比较
System.out.println("autoPacking1.equals(autoPacking2) : "+autoPacking1.equals(autoPacking2));//true 因为两个对象都是Integer时比较的是自动拆箱的值
System.out.println("autoPacking1.equals(newInteger) : "+autoPacking1.equals(newInteger));//true,,此时无论二者的值是不是在Integercache里面//双等比较
System.out.println("autoPacking1==autoPacking2 : "+(autoPacking1==autoPacking2));//true,考虑自动拆箱
System.out.println("autoPacking1==newInteger : "+(autoPacking1==newInteger));//fasle,new关键字的意思是不要当前已经有的,必须新建一个
System.out.println("不在integer的cache中时");//Integer autoPacking3= Integer.valueOf(128);
Integer autoPacking4= 128;
Integer autoPacking5= 128;
Integer newIntger2= new Integer(128);
System.out.println("autoPacking5==autoPacking4 : "+(autoPacking5==autoPacking4));//a 返回false,已经不在cache中所以自动装箱,调用Integer.valueOf(int i)方法.
System.out.println("autoPacking3==autoPacking4 : "+(autoPacking3==autoPacking4));//返回false,因为都是新的对象,所以地址都不一样,都返回false
System.out.println("autoPacking4==newIntger2 : "+(autoPacking4==newIntger2));//返回false
System.out.println("当Integer类型被用来比较大小(>,=,<=)时会先自动拆箱,然后用他们的value比较");
System.out.println(autoPacking4.intValue()==autoPacking4.intValue());//要用双等判断两个对象的值是否相等时,要调用intValue方法
}
@Testpublic voidtestString()
{/*** String 的equals方法中先比较两个对象的地址是否相等,如果地址相等,那么直接返回true,
* 如果地址不相等,遍历字符传,然后判断这两个对象的字符串的值是否相等,如果此时值相等,此时返回true
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}*/System.out.println("提前声明一下,无论什么时候new 一个对象他要表达的意思都是我不要已经有的对象,必须新建一个对象,所以双等时二者时(他们的地址)不会相等的");
String s1= "qq";
String s11= "qq";
String s2= new String("qq");//双等
System.out.println("s1==s11 : "+(s1==s11));//相等的原因是用到了java缓存机制//系统会创建一个内容为qq的String实例,并将这个实例缓存起来,当第二次执行//String s11 = "qq";代码时,系统会先检查缓存中是否有这个一个String实例的内容,与这个"qq"//直接量的字符序列相同,如果在缓存中知道了这样一个String实例,系统会直接让这个引用指向这个String实例//即二者的指向的地址是相等的
System.out.println("s1==s2 : "+(s1==s2));//equals
System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定义可以知道返回true
System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定义可以知道返回true
System.out.println("s1.equals(s11) : "+s1.equals(s11));//true 由equals方法的定义可以知道返回true//String的缓存机制和Integer的常量池是不一样的
}
}