add two integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by blazedaces
    I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

    -blazed
    Just give the example above a try and see for yourself.

    kind regards,

    Jos (old bag of old tricks ;-)

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #17
      Originally posted by JosAH
      Just give the example above a try and see for yourself.

      kind regards,

      Jos (old bag of old tricks ;-)
      Ok, so I tried the following:

      [code=java]
      public class testSomething {

      public static void main(String args[]) {
      Integer a = 42, b = 42;
      Integer c = 129, d = 129;
      System.out.prin tln( a == 42 ); // true
      System.out.prin tln( a == b ); // true
      System.out.prin tln( c == 129 ); // true
      System.out.prin tln( d == 129 ); // true
      System.out.prin tln( c == d ); // false
      System.out.prin tln( c.intValue() == d.intValue() ); // true
      }
      }
      [/code]
      I can only assume from the information thus far that after one byte's value (signed) or at a value greater then or equal to 128 Integer no longer stores its numbers the same way... I do not know why or how.

      I'm trying to think of some other tests to try and understand this...

      Perhaps "==" isn't as simple as I thought it was?

      I don't know...

      -blazed

      Comment

      • mahammadseeraz
        New Member
        • Jul 2007
        • 6

        #18
        Originally posted by blazedaces
        I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

        -blazed
        check this code

        Integer a = 42, b = 42;
        Integer c = 3, d = 3;
        System.out.prin tln( a == 42);
        System.out.prin tln( a == b );
        System.out.prin tln( c == 3 );
        System.out.prin tln( c == d );

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by mahammadseeraz
          check this code

          Integer a = 42, b = 42;
          Integer c = 3, d = 3;
          System.out.prin tln( a == 42);
          System.out.prin tln( a == b );
          System.out.prin tln( c == 3 );
          System.out.prin tln( c == d );
          Yes, now make a and b equal to 142 and try again.

          kind regards,

          Jos

          Comment

          Working...