9 Replies Latest reply: Dec 20, 2006 11:51 PM by 807607 RSS

    Integer == operator?

    807607
      class IntegerTest
      {
      static public void main(String args[] ){
      Integer i1 = 12;
      Integer i2 = 12;
      Integer i3 = 129;
      Integer i4 = 129;
      System.out.println("Result 1 : " + (i1 == i2));
      System.out.println("Result 2 : " + (i3 == i4));
      }
      }

      Output :
      ---------- Run Java Code ----------
      Result 1 : true
      Result 2 : false

      Please help me in how it is giving different output.

      Thanks
        • 1. Re: Integer == operator?
          807607
          what are you tryng to do here?

          are you comparing the values as Integer objects or as primitives?

          here's an example of comparing Integer objects
          Integer inte1=new Integer(1);
          Integer inte2=new Integer(2);
          
          if(inte1.equals(inte2))
             System.out.println("They are equal");
          heres for the primitive
          int int1=1;
          int int2=2;
          
          if(int1==int2)
             System.out.println("They are equal");
          • 2. Re: Integer == operator?
            807607
            I knew that.
            But my questing is how it is behaving in my example.

            Thanks
            • 3. Re: Integer == operator?
              807607
              This is an autoboxing trick. For some reason, when the VM autoboxes
              12, it's using the same object both times. When it autoboxes
              129, it's creating two new objects. In fact, this is the case from
              -128 the way up to 127.

              As far as why, I'm not sure. Performance, maybe? Probably it's
              not coincidence that all the values in Byte exhibit this behaviour.
              public class IntegerTest {
                   static public void main(String args[] ){
                        for (int i = -175; i < 175; i++) {
                        final Integer i1 = i;
                        final Integer i2 = i;
                        System.out.println("Result " + i + " : " + (i1 == i2));
                        }
                   }
              }
              • 4. Re: Integer == operator?
                807607
                I have noticed one thing here!

                Till the integer assigned is in the range of byte, in the range of byte, it returns true,
                after that range it returns false.

                For example replace 129 with some number less than equal to 127, it will return true.
                I have noticed this behavior first time!
                • 5. Re: Integer == operator?
                  800322
                  I have noticed this behavior first time!
                  That's simply because those values are cached since 1.5 and no new instances for those are created unless you explicitely use the new operator.
                  • 6. Re: Integer == operator?
                    796440
                    I have noticed one thing here!

                    Till the integer assigned is in the range of byte, in
                    the range of byte, it returns true,
                    after that range it returns false.

                    For example replace 129 with some number less than
                    equal to 127, it will return true.
                    I have noticed this behavior first time!
                    See reply 3.
                    • 7. Re: Integer == operator?
                      807607
                      That's simply because those values are cached since 1.5 and no new instances for those are created unless you explicitely use the new operator.
                      does autoboxing technique introduced in java VM since 1.5, or it is available in earlier versions too.

                      * and was the intention behind caching only byte range values only was that they take 1byte only. and others take more space, or something else, like some bit manipulations don't get affected in byte range, but in others they do get affected.
                      • 8. Re: Integer == operator?
                        796440
                        does autoboxing technique introduced in java VM since
                        1.5, or it is available in earlier versions too.
                        1.5


                        * and was the intention behind caching only byte
                        range values only was that they take 1byte only.
                        Probably becaue values in that range are very common.
                        • 9. Re: Integer == operator?
                          800322
                          * and was the intention behind caching only byte
                          range values only was that they take 1byte only.
                          Not necessarily, though of course not unlikely that a byte takes a byte. Keep in mind that "byte" defines a value range. Whether the JVM allocates 8 or 32 or 1024 bits for that is probably left to the implementation. booleans are actually defined by the JVM to be represented by an int.

                          But the intention of caching is simply to prevent object creation for autoboxing as much as feasible. If you wanted to save memory space, simply get rid of that cache. The byte value range makes no difference, Integer wraps around an int. You can't go and make that int a byte vor smaller values. That'd make it a different class.