Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Understanding Java Integer

Understanding Java Integer

Scheduled Pinned Locked Moved Java
questionjavacollaborationtutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raptor81
    wrote on last edited by
    #1

    This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.

    L J O 3 Replies Last reply
    0
    • R Raptor81

      This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The following test program shows what happens:

      public class Test {

      /\*\*
       \* @param args
       \*/
      public static void main(String\[\] args) {
          Integer a;
          Integer b;
          a = 10;
          b = 10;
          if (a == b)
              System.out.println("True");
      }
      

      }

      You can also verify by disassembling the class via javap -c.

      Veni, vidi, abiit domum

      J 1 Reply Last reply
      0
      • L Lost User

        The following test program shows what happens:

        public class Test {

        /\*\*
         \* @param args
         \*/
        public static void main(String\[\] args) {
            Integer a;
            Integer b;
            a = 10;
            b = 10;
            if (a == b)
                System.out.println("True");
        }
        

        }

        You can also verify by disassembling the class via javap -c.

        Veni, vidi, abiit domum

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        The following test program shows what happens:

        Errr...I don't think so. And that is demonstrated by changing the '10' to '1000' and then running the program again.

        L 1 Reply Last reply
        0
        • R Raptor81

          This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Raptor81 wrote:

          In my experience, seems that Java does reference comparison with two Integers

          Does exactly that for equality and inequality. If it didn't it wouldn't be backwards compatible.

          Raptor81 wrote:

          and seems that it does value comparison with one Integer and one constant.

          Yes however the problem with doing that is that one can then forget about equality and inequality. And that becomes even more of a problem because java caches integers in the 0 to 128 range. So simple tests can succeed and then fail in production when bigger values are used. (Keep in mind that the range of integers that are cached is not part of the specification so it can change.) So the best idea is to never rely on the operators in the first place. This is demonstrated with the following.

          	Integer a1 = 1000;
          	Integer a2 = 1000;
          	Integer a3 = a1;
          	Integer b1 = 10;  // Less than 128 so Integer is cached.
          	Integer b2 = 10;
          
          	System.out.println("1000 is " + (a1 == a2));
          	System.out.println("10 is " + (b1 == b2));
          
          	System.out.println("1000 != is " + (a1 != a3));
          
          1 Reply Last reply
          0
          • J jschell

            Richard MacCutchan wrote:

            The following test program shows what happens:

            Errr...I don't think so. And that is demonstrated by changing the '10' to '1000' and then running the program again.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You are correct of course; my test was incomplete.

            Veni, vidi, abiit domum

            1 Reply Last reply
            0
            • R Raptor81

              This may be a silly question for Java experts, but even for some one who has some years of Java experience like me, it is still an easily ignored topic. And even the senior developers on our team seem to make mistakes with these. I want to understand the operators on the Java Integer class. What is Java doing when comparing two Integers? For example, let's say we have Integer a and Integer b. Does Java do reference comparison or value comparison on ==, !=, <, >, <=, >= operators? And if one of the operator is an Integer and the other is a constant, what does Java do with ==, !=, <, <>, <=, >=? And if one of the operator is an Integer and the other is an int, what does Java do? In my experience, seems that Java does reference comparison with two Integers, and seems that it does value comparison with one Integer and one constant.

              O Offline
              O Offline
              Oscar0
              wrote on last edited by
              #6

              Integer is not different than any other Java Object (with the previously noted exception that the jvm provides some help to avoid duplication in the smaller values). Perhaps one thing that makes it seem confusing is the auto-conversion between 'int' and Integer. I could see that appearing to be confusing at first. The main thing to remember is that if you want to do numeric comparisons like ==, <, > etc. just make sure you are doing them on 'ints' not Integers.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups