String source="abc";
String str1 = "abc";
String str2 = new String(source);
String str3 = new String("abc");
String str4 = source;
String str5="ab"+"c";
System.out.println(source == str1);//1
System.out.println(source == str2);//2
System.out.println(source == str3);//3
System.out.println(source == str4);//4
System.out.println(source == str5);//5
String str1 = "abc";
String str2 = "abcabc";
String str3 = str1+str1;
String str4 = "abc"+"abc";
final String str5 = "abc";
String str6 = str5+str5;
System.out.println(str2==str3);//6
System.out.println(str2==str4);//7
System.out.println(str2==str6);//8
false
true
true
Integer i1 = 5;
Integer i2 = 5;
System.out.println(i1+"=="+i2+":"+(i1==i2));
i1 = 128;
i2 = 128;
System.out.println(i1+"=="+i2+":"+(i1==i2));
i1 = 127;
i2 = 127;
System.out.println(i1+"=="+i2+":"+(i1==i2));
i1 = -128;
i2 = -128;
System.out.println(i1+"=="+i2+":"+(i1==i2));
i1 = -129;
i2 = -129;
System.out.println(i1+"=="+i2+":"+(i1==i2));
5==5:true
128==128:false
127==127:true
-128==-128:true
-129==-129:false
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
?
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
?
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
?
private IntegerCache() {}
}
Integer int1 = 12911;
Integer int2 = 12911;
System.out.println(int1.equals(int2));//true
Integer i = 1;
short s = 1;
System.out.println(i.equals(s));//false
System.out.println(i == s);//true
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态