Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a 'void' method affect the parameter, and when does it affect the original object?

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

like image 638
Ryan Avatar asked Apr 07 '13 17:04

Ryan


People also ask

Do void methods take parameters?

2.4 Calling a Void Method With Parameters With parameters, the method works like a function in math—different input parameters can give different outputs, but the same set of parameters give the same behavior. If there are no parameters, then the method always has the same behavior.

What happens when you return in a void method?

It exits the function and returns nothing.

What does the void method do?

You use void as the return type of a method (or a local function) to specify that the method doesn't return a value. You can also use void as a referent type to declare a pointer to an unknown type.

Does a void method always return a value?

Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.


1 Answers

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.

like image 62
Jon Skeet Avatar answered Sep 17 '22 14:09

Jon Skeet