0

I have been playing with == in wrapper classes. There's some weird thing that I encountered.

Integer i1 = 3;
Integer i2 = 3;

if(i1 != i2)
{
    System.out.println("i1 and i2 are not equal");     // if condition returns false in this case
}

which is working perfectly fine as far as my knowledge is concerned.

whereas, If I instantiate wrapper with

Integer i1 = 1000;
Integer i2 = 1000;


if(i1 != i2)
{
        System.out.println("i1 and i2 are not equal");    // if condition returns true in here
}

Then I am getting confused with the dual behavior of JVM with respect to the different values assigned to wrapper. Why is this happening so? Please share your thought on this.

Thanks

ankit
  • 438
  • 10
  • 26
  • 1
    dup of http://stackoverflow.com/questions/4638023/strange-wrapper-classes-behavior-with-and?rq=1 – dkatzel Jul 10 '14 at 19:57
  • I don't understand the question.`// if condition returns true in here` in particular, after `i1 != i2` – njzk2 Jul 10 '14 at 19:59

1 Answers1

0

You need to use !i1.equals(i2)

ltalhouarne
  • 4,586
  • 2
  • 22
  • 31