对于程序:
1 public class Collections {
2 public static void main(String[] args) {
3 Integer b = new Integer(0);
4 Integer c = new Integer(0);
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
2 public static void main(String[] args) {
3 Integer b = new Integer(0);
4 Integer c = new Integer(0);
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
result为false,而
1 public class Collections {
2 public static void main(String[] args) {
3 Integer b = 0;
4 Integer c = 0;
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
2 public static void main(String[] args) {
3 Integer b = 0;
4 Integer c = 0;
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
则返回true
1 public class Collections {
2 public static void main(String[] args) {
3 int b= 0;
4 Int c = 0;
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
2 public static void main(String[] args) {
3 int b= 0;
4 Int c = 0;
5 if (b == c) {
6 System.out.println(true);
7 } else {
8 System.out.println(false);
9 }
10 }
11 }
同样返回true。
对于第一种情况
Integer b = new Integer(0);
Integer c = new Integer(0);
Integer c = new Integer(0);
这里的b,c被当作一个对象,而作为对象,b==c仅当b和c所指向的对象是一个对象则为真,而在这里,显然,b和c所指向的是不同的对象。
对于第二,三种情况中的b,c系统当作基本类型来看待,当b,c是基本类型的时候b==c当两者内容相等时,则为真
那么对于int b=0和Integer b = new Integer(0);
浙公网安备 33010602011771号