Pregunta

I've posted two examples below.

In the first example, I use the equal-to operator and the while process continues forever instead of breaking when it should.

In the second example, I use the greater-than-or-equal-to operator, and the process breaks when it should.

How is this even possible?

EXAMPLE 1:

  Integer myVar1 = 42985;
  Integer myVar2 = 1;
  while (true)
  {
     if (myVar2 == myVar1)
     {
     break;
     }

  ++ myVar2;
  }

EXAMPLE 2:

  Integer myVar1 = 42985;
  Integer myVar2 = 1;
  while (true)
  {
     if (myVar2 >= myVar1)
     {
     break;
     }

  ++ myVar2;
  }

EDIT: Thank you everyone for the great answers! I fully understand the problem now and this new information explained several strange behaviors that I've encountered in my apps. I wish I could choose more than one best answer.

¿Fue útil?

Solución

This is one of the less pleasant effects of auto-boxing.

In your first example, the == operator indicates identity equality: the two objects will only be equal if they are the same instance.

In your second example, the '>=' operator indicates numeric comparison: the two objects will be auto-unboxed and then compared.

Making things more confusing, there's a range of "small" integers (-128 <= X <= 127, iirc) for which the JVM caches Integer values, so the == operator sometimes works.

Bottom line: use .equals() and .compareTo().

Otros consejos

You will want to use .equals() to compare the value of two objects.

Anytime you use == it will compare instances of objects. .equals() is a method inherited from Object and is used to compare the values of objects.

>= does not work with objects, only numerics. The Integers are unboxed into ints which can be compared properly.

When using == no unboxing occurs and therefore the references are compared. This is never guaranteed to compare the values.

You can use:

(myVar2.equals(myVar1))

In the EXAMPLE 1, when you use the operator == with two Integer you are comparing their positions in memory, because Integer is a class and variables myVar1 and myVar2 are objects and they are not unboxed in two int primitives, whereas in the EXAMPLE 2, when you use the operator >= the two Integer objects are unboxed and the process breaks when it should. You can use the operator equals:

Integer myVar1 = 42985;
Integer myVar2 = 1;
while (true) {
    if (myVar2.equals(myVar1)) {
        break;
    }


    ++myVar2;
}

Or you can try the method intValue():

Integer myVar1 = 42985;
Integer myVar2 = 1;
while (true) {
    if (myVar2.intValue() == myVar1.intValue()) {
    break;
}

++myVar2;
}

If a Integer object value is equal or less than 127 the operator == will work:

Integer myVar1 = 127;
Integer myVar2 = 1;
while (true) {
    if (myVar2 == myVar1) {
        break;
}


++myVar2;
}

You have to use equals when you compare 2 objects.

if you still want to use >= then auto-unbox (de-autobox) myvar1 and myvar2:

if (myVar2.intValue() >= myVar1.intValue())

A simple best practice for auto-boxing in Java: Wrapper classes should only be used whenever you absolutely cannot use a primitive.

Autounboxing occurs when you use the >= but does not when comparing with == use equalsfor comparing the objects

I recommend you check this question

Use this code for comparing :

 Integer myVar1 = 42985;
 Integer myVar2 = 1;
 while (true)
{
 if (myVar2.equals(myVar1))
 {
 break;
 }

++ myVar2;
}

You are comparing if the references are the same here, you are not comparing primitive types:

if (myVar2 == myVar1)

You should use equals to compare the values:

if (myVar2.equals( myVar1 ) )

For the >= case, this won't work with an Object so it will unbox the values and work as you expect.

In Java, the 'int' type is a primitive , whereas the 'Integer' type is an object.

so to compare objects use equals() method

so either use :

 Integer myVar1 = 42985;
 Integer myVar2 = 1;
 while (true)
  {
    if (myVar2.equals(myVar1))
     {
      break;
     }

     ++ myVar2;
  }

or declare it using int

int myVar1 = 42985;
 int myVar2 = 1;
 while (true)
  {
    if (myVar1==myVar2)
     {
      break;
     }

     ++ myVar2;
  }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top