8 Replies Latest reply on May 2, 2008 7:36 AM by 791266

    Integers vs. ints

    807601
      import java.util.*;
      public class PrimitiveTest
      {
          public static int x;
          public static void main(String[] args)
          {
              x = 5;
              change(x);
              System.out.println(x);
          }
          public static void change(int x)
          {
              x = 7;
          }
      }
      I understand that upon the execution of main, 5 is returned because x is a primitive and change creates an entirely new value.
      import java.util.*;
      public class IntegerTest
      {
          public static Integer x;
          public static void main(String[] args)
          {
              x = new Integer(5);
              change(x);
              System.out.println(x);
          }
          public static void change(Integer x)
          {
              x = 7;
          }
      }
      Upon execution of the main, I expected this to return 7, but it returned 5. I thought that since x is an Integer object, it would change its reference to 7. However, it seems that a new Integer object is created when change is executed. Why?

      Edited by: youmefriend722 on May 1, 2008 11:36 PM
        • 1. Re: Integers vs. ints
          807601
          For starters your change method is void so nothing is returned. The reason you get 5 in both situations is basically for the same reason. You have two different variables called x. In both cases the local variable in change method is changed, thus leaving the local variable in main unchanged. Hence the output of 5.
          • 2. Re: Integers vs. ints
            807601
            Integers, along with all primitive wrappers, are immutable, meaning that there is no way to change the value in Integer objects. You are creating a new object and assigning it to the Integer reference.

            in your second example
            x = 7;
            is kind of just short for
            x = new Integer(7);
            Edited by: spoon_ on May 1, 2008 11:41 PM
            • 3. Re: Integers vs. ints
              807601
              spoon_ wrote:
              x = 7;
              is kind of just short for
              x = new Integer(7);
              Yep! I should have added that.
              • 4. Re: Integers vs. ints
                807601
                Integers, along with all primitive wrappers, are immutable,
                True - but not relevant here.

                As flounder observed the assignment is being made to a local variable and changing one local variable will not affect the value of the other. In general the value of a variable used as an argument in a method call will never change its value as a result of that method. This is true for primitive and reference variables alike.

                (Of course for reference variables the state of the object referenced may be changed - but that's a different matter. One that does involve mutability.)
                • 5. Re: Integers vs. ints
                  807601
                  Thanks for helping me understand the problem with having two variables with the same name (one static, other local). Actually, I think both responses are pertinent because it depends on whether you thought I was trying to do
                  public static void change(Integer n)
                      {
                          n = 7;
                      }
                  in which case the immutable explanation would solve my problem

                  or
                  public static void change(Integer n)
                      {
                          x = 7;
                          //this would kind of be pointless
                      }
                  in which case the explanation about the two variables would solve my problem

                  Thanks to all of you for a quick response.
                  Edited by: youmefriend722 on May 1, 2008 11:53 PM

                  Edited by: youmefriend722 on May 1, 2008 11:55 PM
                  • 6. Re: Integers vs. ints
                    807601
                    test message. please ignore it.
                    • 7. Re: Integers vs. ints
                      807601
                      pbrockway2 wrote:
                      Integers, along with all primitive wrappers, are immutable,
                      True - but not relevant here.

                      As flounder observed the assignment is being made to a local variable and changing one local variable will not affect the value of the other. In general the value of a variable used as an argument in a method call will never change its value as a result of that method. This is true for primitive and reference variables alike.

                      (Of course for reference variables the state of the object referenced may be changed - but that's a different matter. One that does involve mutability.)
                      Yes I guess it also depends on what you thought the autoboxing syntax did. It is conceivable that someone would think incorrectly that the "x = 7" syntax actually meant "x.contents = 7" or something like that. Then that would make Integers mutable, and then it would be relevant.
                      • 8. Re: Integers vs. ints
                        791266
                        spoon_ wrote:
                        Integers, along with all primitive wrappers, are immutable, meaning that there is no way to change the value in Integer objects. You are creating a new object and assigning it to the Integer reference.

                        in your second example
                        x = 7;
                        is kind of just short for
                        x = new Integer(7);
                        This might sound like nitpicking, but It's rather a short hand for Integer.valueOf(7). The difference comes when you are testing for equality.