-2

Can someone explain me the logic why a and b are both equal and x and y are not both equal but they have the same value.

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
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • You shouldn't use `==` to compare objects. Either use `x.intValue()`, compare them with `.equals()` or create them as the primitive type `int` in the first place. – Touniouk Nov 23 '17 at 12:32

1 Answers1

0

it is a mistake to compare objects (or really a bad practice) with the ==, for that most of the objects overwrite the compareTo() , and obvious the Intege is not the exception.