8

I am brand new to programming, as well as to this website, so forgive me if I screw anything up. I'm having a heck of a time figuring out how to properly post my code in here.

package tester;
import java.util.*;
public class Mainclass2 {
    public static void main(String[] args) {

        int y = 3; 
        int[] x = {1, 2, 3, 4};

        editnumbersArray(x);
        editnumbersNotArray(y);

        System.out.println(x[2]); **//now this was changed from 3 to 9...**
        System.out.println(y);    //but this one went unchanged.

    }

    //this accepts 'x[]' into the 'a[]' parameter.
    public static void editnumbersArray(int[] a){
        a[2] = 9;  **//<---why does this one CHANGE the actual x[2] instead of just a[2]?**
    }

    //this accepts 'y' into the 'a' parameter.
    public static void editnumbersNotArray(int a){
        a = 9;  **//<--while this one only changes 'a' instead of 'y'?**
    }

}

So my question is basically typed in there as comments. Why does the array that is passed into the method change the values of the original array (x[]) when the int that is passed into the other method doesnt change? I'm sure it's a simple answer, but when I did my research I couldn't figure out what to search. I don't know what this is called so everything I searched led me the wrong way. Thanks for any help!!

EDIT: Thanks for that analogy with the address! That is by far the best way you could have explained it to me. So basically when you pass an array into a parameter, its passing a reference, not the actual value? So when I make adjustments within my method, its changing whatever the array is referencing? I noticed that this also happens with a list. So the list isnt actually passed by value? It seems as if the array/list itself is basically passed in for editing, no matter what I name it within my method (a[] in this case.)

EDIT http://javadude.com/articles/passbyvalue.htm this page really cleared it up. And sorry for posting a duplicate question. The problem was that I didn't know what I was trying to ask. I had never even heard these terms "pass-by-value/reference", so now I know

Ryan
  • 1,988
  • 4
  • 21
  • 34
  • 1
    One of the better answers to this question, which has been asked in so many forms already: http://stackoverflow.com/a/9404727/680925 – Perception Apr 07 '13 at 17:59
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) –  Apr 07 '13 at 18:04

2 Answers2

13

Changing the value of the parameter itself never affects the argument in Java, because all arguments are passed by value. However, look at this method:

public static void editnumbersArray(int[] a){
    a[2] = 9;
}

That assignment doesn't change the value of the parameter. The value of a is still the same reference, to the same array - it just changes the contents of the array.

Imagine if I wrote my home address on a piece of paper for you. It wouldn't matter what you did to that piece of paper - that wouldn't change where I lived. However, if you visited the address and painted the front door green, without ever changing the piece of paper at all, I would see that change.

It's very important to differentiate between different concepts:

  • A variable is a named storage location; it holds a value, which is always either a primitive value (e.g. an int) or a reference. In my example above, the piece of paper was like the variable.
  • A reference is just a value which allows you to navigate to an object. It's not the object itself. It's like the address on the piece of paper.
  • An object contains other variables. There may be several variables which all have values which are references to the same object. It's like the house in my example: I can write my address on several pieces of paper, but there's only one house.

An array is an object which acts as a container for other variables. So the value of a is just a reference to the array.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    You guys just blew my mind. That makes so much sense, thank you! Hopefully one day I will be able to contribute answers to this site like you guys. Right now i can't even 'vote up' haha. – Ryan Apr 07 '13 at 18:08
  • 1
    @RyanKlassy: Everyone has to start somewhere, and understanding this is one of the most important early steps in Java. Suddenly *everything* may well make more sense - what it means to assign the value of one variable to another, garbage collection etc. – Jon Skeet Apr 07 '13 at 18:13
2

Java uses pass by value (what you want to search for) for everything. Essentially that means it makes a copy of the parameter that it then passes to the method. That means that you cannot change what something points at by using the = operator.

That is why the (int a) version doesn't change a.

However, in the case of an Object or an array it doesn't make a copy of the Object or array, it makes a copy of the reference to the Object or the array. That means that you have two variables, the original on and the, in your example, (int[] a) one that both point to the same spot in memory. Changes to either variable will affect the other variable.

Pass by value, pass by reference, and pass reference by value are the types of things you want to search on for more information.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163