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;
}
}