Equal value judgment of Integer and int

-Use equals instead of == for object equality judgment. Equals is defined for classes that require equality judgment (should).

・ I sometimes see a program that determines the equality of Strings with ==, but when I see it, I am likely to become anemic. Debugging is passing for the time being. The reason this works is that it will be true if it is turned by assignment.

-Although it is the same for Integer, it is a little complicated because there is a primitive (int).

・ Check including common sense.

-As a result, it should be noted that when == is used in the equality judgment between Integers. At the time of equality judgment, it is not checked one by one whether the target variable is Integer or int. And in most cases = = Is used. This is dangerous ....

-But it seems very unlikely that both are Integers. Good, good.

Tips0048.java


package jp.avaj.lib.algo;

import jp.avaj.lib.test.ArTest;
import jp.avaj.lib.test.L;

public class Tips0048 {
  public static void main(String[] args) {
    //Start a test case
    ArTest.startTestCase("Tips0048");

    L.p("\n====Integer and Integer equality judgment");
    {
      Integer i0 = new Integer(5);
      Integer i1 = i0;
      Integer i2 = new Integer(5);
      //
      //Since this is an assignment, it will be true.
      ArTest.isTrue("i0 vs i1","result",(i0 == i1));

      //This is false ⇒ ★ Only this is dangerous
      ArTest.isFalse("i0 vs i2","result",(i0 == i2));
    }
    L.p("\n====Equal value judgment of Integer and int");
    {
      Integer integer = new Integer(5);
      int int0 = 5;
      //
      //Judgment by equals
      ArTest.isTrue("Integer.equals(int)","result",integer.equals(int0));
      // ==Judgment by
      ArTest.isTrue("Integer == int","result",integer == int0);
    }
    L.p("\n====Equal value judgment of int and int ⇒ Omitted");
    //
    L.p("\n====Integer and Integer size judgment");
    {
      Integer integer0 = new Integer(5);
      Integer integer1 = new Integer(4);
      ArTest.isTrue("Integer > Integer","result",integer0 > integer1);
    }
    L.p("\n====Large / small judgment of Integer and int");
    {
      Integer integer0 = new Integer(5);
      ArTest.isTrue("Integer > int","result",integer0 > 4);
    }
    L.p("\n====Large / small judgment of int and int ⇒ Omitted");


    L.p("\n====Extra");
    //Both of these are false. It's natural, but it feels very strange.
    L.p((new Integer(1)).equals(new Long(1))+"");
    L.p(((new Boolean(true)) == (new Boolean(true)))+"");

    L.p("");
    //End the test case
    ArTest.endTestCase();
  }
}

The execution result is as follows.

**** Tips0048 start ****

====Integer and Integer equality judgment
OK i0 vs i1:result=true
OK i0 vs i2:result=false

====Equal value judgment of Integer and int
OK Integer.equals(int):result=true
OK Integer == int:result=true

====Equal value judgment of int and int ⇒ Omitted

====Integer and Integer size judgment
OK Integer > Integer:result=true

====Large / small judgment of Integer and int
OK Integer > int:result=true

====Large / small judgment of int and int ⇒ Omitted

====Extra
false
false

**** Tips0048 summary ****
test count = 6
success    = 6

Recommended Posts

Equal value judgment of Integer and int
Judgment of JSONArray and JSONObject
Same judgment / equal value judgment / comparison / order in Swift and Java
[Java] Judgment of identity and equivalence
Difference between int and Integer in Java
Please note the division (division) of java kotlin Int and Int
Judgment of the calendar
Setting value of log4jdbc.properties