You already have some good answers, but here's a bit of code showing the two cases that may seem to be inconsistent, but are indeed explained by the fact that java is pass-by-value. The tricky bit is that in the case of arrays, it's the reference to the array that's being passed by value, not the array itself.
Hence the called function receives a copy of the reference to the same array as the caller function, and can modify elements within that array. But when the called function modifies the reference itself to refer to a different array it is modifying a copy, which has no effect on the caller --- that is, in the caller environment the variable is still referring to the original array.
This is easier to explain with boxes and arrows :-), but hopefully the code and output below will be helpful:
$ cat PBV.java
class PBV
{
private static void modfiyArrayElement(int[] intArray) {
// intArray is referring to the same array as in main
intArray[0] = 17;
}
public static void main(String[] args) {
int[] a = new int[]{ 1, 2, 3 };
System.out.println(a[0]);
modifyArrayElement(a);
System.out.println(a[0]);
}
}
$ java PBV
1
17
$ cat PBV2.java
class PBV2
{
private static void modfiyArrayReference(int[] intArray) {
System.out.println("\nIn modifyArrayReference:");
System.out.println("intArray[0] is " + intArray[0]);
System.out.println("ref value of intArray is: " + intArray);
intArray = new int[] { 100, 200, 300 };
// intArray is no longer referring to the same array as in main!
// at this point munging with intArray won't have an effect in main
System.out.println("\nintArray[0] is now " + intArray[0]);
System.out.println("ref value of intArray is: " + intArray +"\n");
}
public static void main(String[] args) {
System.out.println("in main:");
int[] a = new int[]{ 1, 2, 3 };
System.out.println("a[0] is " + a[0]);
System.out.println("ref value of a is: " + a);
modfiyArrayReference(a);
System.out.println("back in main:");
System.out.println("a[0] is still " + a[0]);
System.out.println("ref value of a is still: " + a);
}
}
$ java PBV2
in main:
a[0] is 1
ref value of a is: [I@55a6c368
In modifyArrayReference:
intArray[0] is 1
ref value of intArray is: [I@55a6c368
intArray[0] is now 100
ref value of intArray is: [I@37670cc6
back in main:
a[0] is still 1
ref value of a is still: [I@55a6c368
doStuffinto variabledagain.