Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: manipulating collections as/from parameter/arguments

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default manipulating collections as/from parameter/arguments

    the title of the post sounds a little bit confusing and i dont even know what title should i give on this critical thing i just encountered ,

    this 1st code
    public class ManipulateData {
     
        public static void main(String[] args) {
     
            int d = 1;
            change(d); // this will change a value 
            print(d);    // im expecting that this method will print the copy value of 1
        }
     
        public static void change(int d) {
     
            d = 10;
        }
     
        public static void print(int d) {
     
            System.out.println(d);
        }
    }
    prints 1 because the passing of the value came from a separate call of the methods, even though it has been changed

    but. when it comes to collection
    public class ManipulateArrays {
     
        public static void main(String[] args) {
     
            int[] a = {1};
     
            // 2 methods being called, and 2 separate passes of the array value
     
            change(a);  
     
            // i was expecting that this method will still print out the copy value of 1,but it prints the changed value
            // from a separate call of a method from which it was passed
            print(a);
        }
     
        public static void change(int[] a) {
     
            a[0] = 99;
        }
     
        public static void print(int[] a) {
     
            System.out.println(a[0]);
        }
    }

    it prints the value even though it has been passed on a separate method call, i was thinking at first, that maybe its because a static call, that it has to do something with class data, but i tried to make everything as an instance call, the flow is still the same..

    simple question is,... why does the array is being changed even on a separate passes to different methods?(sorry for bad english)
    Last edited by chronoz13; October 1st, 2011 at 10:34 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: manipulating collections as/from parameter/arguments

    Java passes arguments by value. The value of the variable is copied to a new location and that is given to the method.The original value of the variable is not reachable via the passed value.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    The original value of the variable is not reachable via the passed value.
    but why does the values of the array is being changed even on separate calls, just like the sample codes above?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: manipulating collections as/from parameter/arguments

    why does the values of the array is being changed
    A copy of the reference to the array is passed. It's that reference can't be changed.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    it still makes me confuse >.<, i do understand somehow about the passing / copy of values , primitive or reference. but its still a big ? to me about the values/value of that array

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

     
    int[] array = {1}; // here the value of array is 1
     
    change(array); // here the value of the array is being changed(separate call of method)
    print(array);    // it prints the changed value of the array(separate call)

    it differs this one, this one below, is easy to follow the flow of the calling of the methods..and passing of the value
     
    int aNumber = 1;
     
    change(aNumber); // it does some changes here(separate call)
    print(aNumber);    // it prints a value, which is 1 (A Separate call again)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: manipulating collections as/from parameter/arguments

    Do you understand how the compiler assigns memory locations to hold the value of a variable?
    If you have this concept then I can explain what happens when the you call a method with some arguments.

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: manipulating collections as/from parameter/arguments

    Arrays are not part of Java Collections, and neither are they Primitives:
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Arrays are a 'special' data type somewhere between a primitive and an object.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

  9. The Following User Says Thank You to Sean4u For This Useful Post:

    chronoz13 (October 1st, 2011)

  10. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    Do you understand how the compiler assigns memory locations to hold the value of a variable?
    somehow, i do understand that for example (int x = 1), the compiler will allocate a memory space as an integer type and assign a value of 1, in case of objects
    like String as reference for example (String word = "Hello"), compiler allocates 2 memory , 1 for the variable 1 for the value of the String, and refers the value to the variable, as for passing arguments to parameters, just like what you have said, and what i read before, values are not exactly being passed, its their copies that is being sent to parameters.

    Arrays are not part of Java Collections
    thank you Sean for enlightening me.. i do know that arrays are not primitive nor an object, but instead a reference.. that is being reffered to an identifier of same type,

  11. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    i just found an article, explaining that , the reference is being passed, so all the methods that receives that array argument, will refer to the ORGININAL array location?. so no matter how many separate calls a program will make to an array, all of those calls are reffering to the original one?

    Beginning Java - Unit 6 Arrays - Arrays and Methods

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: manipulating collections as/from parameter/arguments

    A copy of the reference to the array is passed to each method so they can manipulate the contents of the array.

  13. The Following User Says Thank You to Norm For This Useful Post:

    chronoz13 (October 1st, 2011)

  14. #12
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    so somehow we can say that arrays although as a reference.. is mutable?

  15. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: manipulating collections as/from parameter/arguments

    thank you norm.

Similar Threads

  1. Reg: Collections
    By vddmanikanta in forum Collections and Generics
    Replies: 1
    Last Post: July 17th, 2011, 09:33 AM
  2. Collections
    By pokuri in forum Collections and Generics
    Replies: 4
    Last Post: June 3rd, 2011, 09:08 AM
  3. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  4. collections
    By bardd in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2011, 09:31 AM
  5. Help me to Choose Collections
    By kathir0301 in forum Collections and Generics
    Replies: 1
    Last Post: December 3rd, 2010, 10:14 AM