Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321; System.out.println(c == d);
System.out.println(e == f);输出 true false
Integer 相等Java
Integer d = 3;
Integer e = 321;
Integer f = 321; System.out.println(c == d);
System.out.println(e == f);输出 true false
Integer 相等Java
javap指令怎么用?
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
这是源码中的,也就是说cache中已有-128到127,不在这范围的会新new ,这时可以理解比较是内存地址,也就是是不是同一对象,并不是比较hashCode,hashcode相同对象不一样相同,毕竟hashcode是int大小是有限的,看来lz的==,equal,hashcode什么的还有待提高哈。。
而e,f是对象,此时用e==f比较的是e,f所指向的内存地址,结果是false。
对象的比较时,要覆写hashcode和equals方法。记得以前时是这么看到的。
故以为hashcode可以像地址一样表示唯一性,原来后来的类有的会重写hashCode方法,
那么就不能根据hashCode值来保证地址的唯一性
其实hashCode跟equals方法保持高度的一致性,hashCode值的唯一性跟equals方法
判断出的唯一性是一致的,故若子类重写了equals方法必重写hashCode方法,
这次说对了吧?你们这群人,现在体罚学生都是犯法的,何况体罚楼主
还有一题,各位若感兴趣,给个解释
public static void main(String[] args)
{
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
modifyPoint(p1, p2);
System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
} private static void modifyPoint(Point p1, Point p2)
{
Point tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;
p1.setLocation(5, 5);
p2 = new Point(5, 5);
}P1为何是[0,0]?
String s1 = "abcde";
String s3 = "abcde";
String s2 = new String("abcde");
this.x = x;
this.y = y;
}
<1>. private static void modifyPoint(Point p1, Point p2)
{
/* Point tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;*/
p1.setLocation(5, 5);
p2 = new Point(5, 5);
}
结果是[5,5], [0,0]<2>. private static void modifyPoint(Point p1, Point p2)
{
Point tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;
p1.setLocation(5, 5);
p2 = new Point(5, 5);
}
结果是[0,0], [5,5]咋回事?哪位大师解释一下?
Java的对象在堆中,Java在方法在执行时,会为每个方法创建栈帧,并压入当前线程的栈,每个栈帧都保存有方法的变量,字节码,引用等信息,以<1>为例子,P1,P2在main方法的栈帧中很老实的指向堆中的两个对象。而在modifyPoint栈帧中,p2 = new Point(5,5);创建了一个新的对象,并将该栈帧中的p2指向堆中的新对象。
注意,此问题的根本原因是两个方法中的P2是指向同一个堆对象的不同引用,对引用本身的更改,在不同的栈帧中是不相影响的。
对于<2>也同样分析即可。
p2 = new Point(5, 5);
有什么不同呢?
而 p2 = new Point(5, 5);则修改了引用P2的值,执行前和扫行后,引用P2指向的堆内存区域是不同的
你打印一下这两个引用的值就明白了
System.out.println(p1);
p1.setLocation(5, 5);
System.out.println(p1);
System.out.println(p2);
p2 = new Point(5, 5);
System.out.println(P2);
static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
public static void main(String[] args)
{
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
Test1 test1 = new Test1();
System.out.println(p1);
System.out.println(p2);
test1.modifyPoint(p1, p2);
System.out.println(p1);
System.out.println(p2);
System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
} public void modifyPoint(Point p1, Point p2)
{
/*Point tmpPoint = p1;
p1 = p2;
p2 = tmpPoint;*/
System.out.println("P1 IN modifyPoint:" + p1);
p1.setLocation(5, 5);
System.out.println("P1 IN modifyPoint:" +p1);
System.out.println("P2 IN modifyPoint:" +p2);
p2 = new Point(5, 5);
System.out.println("P2 IN modifyPoint:" +p2);
}
结果是:
refOrValue.Point@2f9ee1ac
refOrValue.Point@67f1fba0
P1 IN modifyPoint:refOrValue.Point@2f9ee1ac
P1 IN modifyPoint:refOrValue.Point@2f9ee1ac
P2 IN modifyPoint:refOrValue.Point@67f1fba0
P2 IN modifyPoint:refOrValue.Point@3fbefab0
refOrValue.Point@2f9ee1ac
refOrValue.Point@67f1fba0
[5,5], [0,0]
{
Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
Test1 test1 = new Test1();
test1.modifyPoint(p1, p2);
System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
} public void modifyPoint(Point mp1, Point mp2)
{
mp1.setLocation(5, 5);
mp2 = new Point(5, 5);
}