Why Java does not see that Integers are equal?

后端 未结 6 1785
野的像风
野的像风 2020-11-29 06:51

I have integers that are supposed to be equal (and I verify it by output). But in my if condition Java does not see these variables to have the same value.

相关标签:
6条回答
  • 2020-11-29 07:00

    In java numeric values within range of -128 to 127 are cached so if you try to compare

    Integer i=12 ;
    Integer j=12 ; // j is pointing to same object as i do.
    if(i==j)
       print "true";
    

    this would work, but if you try with numbers out of the above give range they need to be compared with equals method for value comparison because "==" will check if both are same object not same value.

    0 讨论(0)
  • 2020-11-29 07:04

    The condition at

    pay[0]==point[0]
    

    expression, uses the equality operator == to compare a reference

    Integer pay[0]
    

    for equality with the a reference

    Integer point[0]
    

    In general, when primitive-type values (such as int, ...) are compared with == , the result is true if both values are identical. When references (such as Integer, String, ...) are compared with == , the result is true if both references refer to the same object in memory. To compare the actual contents (or state information) of objects for equality, a method must be invoked. Thus, with this

    Integer[] point = new Integer[2];
    

    expression you create a new object that has got new reference and assign it to point variable.

    For example:

    int a = 1;
    int b = 1;
    Integer c = 1;
    Integer d = 1;
    Integer e = new Integer(1);
    

    To compare a with b use:

    a == b
    

    because both of them are primitive-type values.

    To compare a with c use:

    a == c
    

    because of auto-boxing feature.

    for compare c with e use:

    c.equals(e)
    

    because of new reference in e variable.

    for compare c with d it is better and safe to use:

      c.equals(d)
    

    because of:

    As you know, the == operator, applied to wrapper objects, only tests whether the objects have identical memory locations. The following comparison would therefore probably fail:

    Integer a = 1000;
    Integer b = 1000;
    if (a == b) . . .
    

    However, a Java implementation may, if it chooses, wrap commonly occurring values into identical objects, and thus the comparison might succeed. This ambiguity is not what you want. The remedy is to call the equals method when comparing wrapper objects.

    0 讨论(0)
  • 2020-11-29 07:10

    Check out this article: Boxed values and equality

    When comparing wrapper types such as Integers, Longs or Booleans using == or !=, you're comparing them as references, not as values.

    If two variables point at different objects, they will not == each other, even if the objects represent the same value.

    Example: Comparing different Integer objects using == and !=.

    Integer i = new Integer(10);
    Integer j = new Integer(10);
    System.out.println(i == j); // false
    System.out.println(i != j); // true
    

    The solution is to compare the values using .equals()

    Example: Compare objects using .equals(…)

    Integer i = new Integer(10);
    Integer j = new Integer(10);
    System.out.println(i.equals(j)); // true
    

    …or to unbox the operands explicitly.

    Example: Force unboxing by casting:

    Integer i = new Integer(10);
    Integer j = new Integer(10);
    System.out.println((int) i == (int) j); // true
    

    References / further reading

    • Java: Boxed values and equality
    • Java: Primitives vs Objects and References
    • Java: Wrapper Types
    • Java: Autoboxing and unboxing
    0 讨论(0)
  • 2020-11-29 07:12

    If they were simple int types, it would work.

    For Integer use .intValue() or compareTo(Object other) or equals(Object other) in your comparison.

    0 讨论(0)
  • 2020-11-29 07:16

    There are two types to distinguish here:

    • int, the primitive integer type which you use most of the time, but is not an object type
    • Integer, an object wrapper around an int which can be used to use integers in APIs that require objects
    0 讨论(0)
  • 2020-11-29 07:19

    when you try to compare two objects (and an Integer is an object, not a variable) the result will always be that they're not equal,

    in your case you should compare fields of the objects (in this case intValue)

    try declaring int variables instead of Integer objects, it will help

    0 讨论(0)
提交回复
热议问题