When I passing an object reference (arrays are objects) to a method, the value is passed, right?
In the following code the initial value is: 333. After method passing, the value is changed. Why? Is in this case the reference passed instead the value?
Are arrays "special cases"?
public static void main(String[] args) {
int[] myArray = { 333 };
valueOrRef(myArray); // Value or Reference?
System.out.println(myArray[0]); // 777 (changed)
}
public static void valueOrRef(int[] myArgument) {
myArgument[0] = 777;
}
Another approach: (the logic "pass by value"):
public static void main(String[] args) {
int[] myArray = { 333 };
valueOrRef(myArray[0]); // Value or Reference?
System.out.println(myArray[0]); // 333 (after method still unchanged)
}
public static void valueOrRef(int myArray2) {
myArray2 *= 2;
}