0

I'm confused about the method. I thought typically you need to return something. Or in my mind I would have thought the need for a for loop that passes the elements back from the method that adds 5 to each element.

However When the array is passed to the method, the array itself changes, but why?

public static void main(String[] args) {
       int ray[]={3,4,5,6,7};
       change(ray);

       for(int y: ray){
           System.out.println(y);
       }
    }
    public static void change(int x[]){
        for(int counter = 0; counter < x.length;counter++){
            x[counter]+=5;
        }
    }

2 Answers2

0

In Java, all objects are passed by reference, and arrays are objects. That means that your change method receives the same array that was created in main--not a copy of it, the same object. So when you modify it, the main method sees that change too.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23
  • [Java is *always* pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Andy Thomas Jan 11 '16 at 19:11
0

Java is pass-by-value for primitives, and pass-by-reference(value) for everything else (including arrays).

http://javadude.com/articles/passbyvalue.htm

What that basically means is that your function does not get a copy of the array, it gets the array itself.

Try it with an int (the original value will not change, because it's a primitive).

public static void main(String[] args) { int ray[]={3,4,5,6,7}; change(ray);

   for(int y: ray){
       System.out.println(y);
   }


}

public static void change(int i){
      i = i + 10;
}



public static void change(int x[]){
    for(int counter = 0; counter < x.length;counter++){
        x[counter]+=5;
    }
}

Some will say that Java always passes by value, but that's because of a poor choice of definitions when it comes to references.

Objects are conceptually passed by reference, and primitives by value. You can call it what you like but it looks like a duck, walks like a duck and quacks like a duck.

Try this, you'll get a better idea:

/**
 * Main 
 * 
 */
public class Main {

    static class IntegerClass {
        int internal;

        public IntegerClass(int i) {
            internal = i;
        }

        public void setInternal(int i) {
            internal = i;
        }

        public int getInternal() {
            return internal;
        }
    }

    public static void main(String[] a) {
        int x = 10;
        changeInteger(x);
        System.err.println(x);

        IntegerClass ic = new IntegerClass(10);
        changeIntegerClass(ic);
        System.err.println(ic.getInternal());
    }

    public static void changeIntegerClass(IntegerClass ic) {
        ic.setInternal(500);
    }

    public static void changeInteger(Integer i) {
        i = 500;
    }
}
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • 1
    [Java is *always* pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Andy Thomas Jan 11 '16 at 19:11
  • Actually, java isn't pass-by-reference on objects. It is pass-by-value. The only thing that changes is that, with objects, the reference gets passed by value, so you'll **not** be able to modify the object itself, but you'll be able to modify every property of the objects. So in case of arrays, you'll not be able to change the array itself, you'll be able to change the values stored in the array. In your example, that means that you cannot re-assign `x` with a new array, you can only change its contents. – BackSlash Jan 11 '16 at 19:17
  • See my edit, saying java always passes by value is like saying planes can't fly but not telling anyone that by fly you really mean swim. – mikeb Jan 11 '16 at 19:18
  • @mikeb No, it's not the same. If an object is pass-by-reference, it means that you **can modify the reference**. In java you **cannot** modify the reference, that's why it's important to note that java passes references by value. In languages like C, C++ and similar, you can modify references, so you can edit everything is passed to a method. In java you can't. See [this example](http://ideone.com/0APOEq). To refer to your last example, the method `changeInteger` would have been able to change the value of `i` if the object was pass-by-reference. – BackSlash Jan 11 '16 at 19:23
  • Thank you guys. I understand now! – MrTumble Jan 11 '16 at 23:33
  • Thank you guys. I understand now! – MrTumble Jan 11 '16 at 23:33
  • Thank you guys. I understand now! – MrTumble Jan 11 '16 at 23:33