add two integers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sridhar19
    New Member
    • Jul 2007
    • 1

    #1

    add two integers

    i need add two integers in java codeing
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sridhar19
    i need add two integers in java codeing
    Are you having any specific problems with it? Does the user have to supply the integers or are hardcoding the integers to be added in there.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by sridhar19
      i need add two integers in java codeing
      For primitive 'int's your question is hilarious; for Integers have a look at autoboxing,
      i.e. starting at Java 1.5 you can simply do this:

      [code=java]
      Integer a= new Integer(41);
      Integer b= new Integer(13);
      Integer c;

      c= a+b;
      [/code]

      For Java < 1.5 you have to do this:

      [code=java]
      Integer a= new Integer(41);
      Integer b= new Integer(13);
      Integer c;

      c= new Integer(a.intVa lue()+b.intValu e());
      [/code]

      kind regards,

      Jos

      Comment

      • blazedaces
        Contributor
        • May 2007
        • 284

        #4
        Originally posted by JosAH
        For primitive 'int's your question is hilarious; for Integers have a look at autoboxing,
        i.e. starting at Java 1.5 you can simply do this:

        [code=java]
        Integer a= new Integer(41);
        Integer b= new Integer(13);
        Integer c;

        c= a+b;
        [/code]

        For Java < 1.5 you have to do this:

        [code=java]
        Integer a= new Integer(41);
        Integer b= new Integer(13);
        Integer c;

        c= new Integer(a.intVa lue()+b.intValu e());
        [/code]

        kind regards,

        Jos
        I didn't know this, just tested it out, but you can even do the following:

        [code=java]
        public class testIntegerAddi ng {

        public static void main(String args[]) {
        Integer a = new Integer(13);
        Double b = new Double(12.5);

        Double c = a + b;
        System.out.prin tln(c);
        }
        }
        [/code]

        -blazed

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by blazedaces
          I didn't know this, just tested it out, but you can even do the following:

          [code=java]
          public class testIntegerAddi ng {

          public static void main(String args[]) {
          Integer a = new Integer(13);
          Double b = new Double(12.5);

          Double c = a + b;
          System.out.prin tln(c);
          }
          }
          [/code]

          -blazed
          Autoboxing is a disease; have a look at this:

          [code=java]
          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( c == d ); // false
          [/code]

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Autoboxing is a disease; have a look at this:

            [code=java]
            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( c == d ); // false
            [/code]

            kind regards,

            Jos
            Hey, I can't test this now but does a == 10 really give true?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              Hey, I can't test this now but does a == 10 really give true?
              Ten? I don't see no ten in my post! :-P seriously though, that was a typo; I
              remembered that silly example and typed it in from the top of my head.
              10 should be 42. The results still are surprising.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by JosAH
                Ten? I don't see no ten in my post! :-P seriously though, that was a typo; I
                remembered that silly example and typed it in from the top of my head.
                10 should be 42. The results still are surprising.

                kind regards,

                Jos
                Yep, when both operands are reference types no autoboxing occurs.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by r035198x
                  Yep, when both operands are reference types no autoboxing occurs.
                  That's not the reason: auto(un)boxing only occurs when one of the types isn't
                  a reference type, e.g. Integer vs int. Here's a better example:

                  [code=java]
                  public boolean check(Integer x, Integer y) {

                  if (!(x < y || y < x))
                  return x == y; // most certainly x == y
                  else
                  return false // most certainly x != y
                  }
                  ...
                  Integer x= 42, y= 42;
                  System.out.prin tln(check(x, y)); // true;

                  Integer x= 142, y= 142;
                  System.out.prin tln(check(x, y)); // false;
                  [/code]

                  kind regards,

                  Jos ;-)

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JosAH
                    That's not the reason: auto(un)boxing only occurs when one of the types isn't
                    a reference type, e.g. Integer vs int. Here's a better example:

                    [code=java]
                    public boolean check(Integer x, Integer y) {

                    if (!(x < y || y < x))
                    return x == y; // most certainly x == y
                    else
                    return false // most certainly x != y
                    }
                    ...
                    Integer x= 42, y= 42;
                    System.out.prin tln(check(x, y)); // true;

                    Integer x= 142, y= 142;
                    System.out.prin tln(check(x, y)); // false;
                    [/code]

                    kind regards,

                    Jos ;-)
                    I still don't see how my reason is different from the one you gave.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by r035198x
                      I still don't see how my reason is different from the one you gave.
                      It's not about autoboxing itself: the puzzle is about why is 42 == 42 true while
                      142 == 142 is false. for both cases (x < y || y < x) is false (so mathematically
                      speaking x == y should be true.

                      kind regards,

                      Jos

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Oh yeah, I've tested it now with

                        [CODE=java]class Test {
                        public static void main(String[] args) {
                        Integer x= 127, y= 127;
                        System.out.prin tln(check(x, y));
                        Integer a= 128, b= 128;
                        System.out.prin tln(check(a, b));
                        }
                        public static boolean check(Integer x, Integer y) {
                        if (!(x < y || y < x))
                        return x == y;
                        else
                        return false ;
                        }
                        }[/CODE]
                        I should have known you were up to your tricks again.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by r035198x
                          Oh yeah, I've tested it now with

                          [CODE=java]class Test {
                          public static void main(String[] args) {
                          Integer x= 127, y= 127;
                          System.out.prin tln(check(x, y));
                          Integer a= 128, b= 128;
                          System.out.prin tln(check(a, b));
                          }
                          public static boolean check(Integer x, Integer y) {
                          if (!(x < y || y < x))
                          return x == y;
                          else
                          return false ;
                          }
                          }[/CODE]
                          I should have known you were up to your tricks again.
                          Cute eh? That's why I wrote that autoboxing is a disease. ;-)

                          kind regards,

                          Jos

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by JosAH
                            Cute eh? That's why I wrote that autoboxing is a disease. ;-)

                            kind regards,

                            Jos
                            It has always looked suspicious.
                            I'm always suspicious of everything that begins with auto. Except for automobile of course.

                            Comment

                            • blazedaces
                              Contributor
                              • May 2007
                              • 284

                              #15
                              Originally posted by JosAH
                              It's not about autoboxing itself: the puzzle is about why is 42 == 42 true while
                              142 == 142 is false. for both cases (x < y || y < x) is false (so mathematically
                              speaking x == y should be true.

                              kind regards,

                              Jos
                              I didn't try it... but why does 142 == 142 output false? Btw, is autoboxing the process which checks "=="? I'm confused...

                              -blazed

                              Comment

                              Working...