5 Replies Latest reply: Feb 13, 2008 2:59 PM by 807601 RSS

    Modifying values in primitive data types?

    807601
      When I pass an integer from my main method to another method, it passes by value and the original variable is left unchanged. My question is: How do you change the value of the original variable, and not just the 'copy' of that variable in my other method?

      I would also appreciate it if you could explain this in 'dumbed down' way since I'm quite new to Java and haven't got all the proper terminology down yet.

      Thanks!
        • 1. Re: Modifying values in primitive data types?
          807601
          Well generally you should be returning values in methods, rather than attempting to have the method actually change the value of a passed parameter.
          • 2. Re: Modifying values in primitive data types?
            807601
            Then please, could you tell me how to 'return values'? I just want the variable I passed into this method, to be changed by the end of the method so that I can call on this variable in other methods, and it will give me a value different from the original value.

            Ex. If I made a variable called int temp = 0 and then passed it into said method, which changes the value to temp = 4, I want the value of temp to remain at 4 and not return to 0 when I exit the method.

            Perhaps i'm not explaining my question properly?
            • 3. Re: Modifying values in primitive data types?
              807601
              Perhaps i'm not explaining my question properly?
              You're explaining it properly, but the behavior you're looking for is deliberately not available in Java. Check uj's example; she has shown you how to get the desired result.

              ~
              • 4. Re: Modifying values in primitive data types?
                807601
                C/C++ used pointers to allow functions to change values that were passed in as parameters. This was a very powerful and flexible system, but very very dangerous. It is quite easy in C to accidentally overwrite data, or create memory leaks and dangling pointers that point to nowhere, or create all sorts of problems that are a nightmare to debug.

                Any debugging problem you've had in Java pales in comparison to debugging a standard pointer error (/exaggeration). Java intentionally took away this flexibility to allow for more easily-readable code, less run-time errors, and the automatic garbage-collection saved us from a large majority of memory leaks.

                Whether or not you understand what I said above...just know that you should be thankful that you need to make methods return a value.
                • 5. Re: Modifying values in primitive data types?
                  807601
                  just know that you should be thankful that you need to make methods return a value.
                  Methods needn't return a value. Methods can manipulate objects by virtue of local copies of object references.

                  ~