我是一个java新手,对下面的例子很困惑。可以认为“==”符号将比较整数之间的值和来自int的“自动装箱”整数之间的值,并比较整数之间的引用地址吗?
双打和0/0呢?
import edu.princeton.cs.introcs.*;
public class Autoboxing {
public static void cmp(Integer first, Integer second) {
if (first < second)
StdOut.printf("%d < %d\n", first, second);
else if (first == second)
StdOut.printf("%d == %d\n", first, second);
else if (first > second)
StdOut.printf("%d > %d\n", first, second);
else
StdOut.printf("%d and %d are incomparable\n", first, second);
}
public static void main(String[] args) {
cmp(new Integer(42), 43);
cmp(new Integer(42), new Integer(42));
cmp(43, 43);
cmp(142, 142);
Integer a0 = 1000;
int b0 = 1000;
Integer c0 = 1000;
StdOut.println("a0==b0?" + (a0==b0));
StdOut.println("a0==c0?" + (a0==c0));
StdOut.println("b0==c0?" + (b0==c0));
double x1 = 0.0, y1 = -0.0;
Double a1 = x1, b1 = y1;
StdOut.println(x1 == y1);
StdOut.println(a1.equals(b1));
double x2 = 0.0/0.0, y2 = 0.0/0.0;
Double a2 = x2, b2 = y2;
StdOut.println(x2 != y2);
StdOut.println(!a2.equals(b2));
}
}
结果是:
42 < 43
42 and 42 are incomparable
43 == 43
142 and 142 are incomparable
=====
a0==b0?true
a0==c0?false
b0==c0?true
=====
true
false
=====
true
false