Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有人能给我解释一下这个逻辑吗?看来两者是相等的_Java - Fatal编程技术网

Java 有人能给我解释一下这个逻辑吗?看来两者是相等的

Java 有人能给我解释一下这个逻辑吗?看来两者是相等的,java,Java,有人能给我解释一下为什么a和b都相等,而x和y不都相等,但它们有相同的值 Integer a = 40;`// integer a Integer b =40; // integer b Integer x = 400; // why x and y are not equal Integer y = 400; // how is that possible if (a==b){ //first condition System.out.println("a=b"); //if

有人能给我解释一下为什么a和b都相等,而x和y不都相等,但它们有相同的值

Integer a = 40;`// integer a 
Integer b =40; // integer b
Integer x = 400; // why x and y are not equal
Integer y = 400; // how is that possible

if (a==b){  //first condition 
   System.out.println("a=b");  //if true print
}else {
   System.out.println("a!=b"); // if not true 
}

if(x==y){  // second condition 
   System.out.println("x=y"); // if true print 
}else{
   System.out.println("x!y"); // if not true print
}

将对象与==进行比较是一个错误(或者实际上是一个糟糕的做法),因为大多数对象都会覆盖
compareTo()
,显然
整数也不例外。

您不应该使用
=
来比较对象。使用
x.intValue()
,将它们与
.equals()
进行比较,或者首先将它们创建为基元类型
int
。或者在任何情况下,将它们创建为equals()