Parameter Passing in Java

In Java, all parameters are passed by value, but for arrays and objects of classes only a reference to the actual parameter, which really is a pointer, is passed. Therefore changing an array element or a class field inside the function does change the actual parameter's element or field. Being a programmer, this demands extra care from you. This would become clearer as you go through the following code examples of parameter passing by value and parameter passing by reference in Java:

PARAMETER PASSING BY REFERENCE:
void f( int [ ] A ) {           
   A[0] = 10;            // change an element of parameter A
}

void g() {
   int [ ] B = new int [3];
   B[0] = 5;
   f(B);
                                              // B[0] is now 10, because function f changed the
                                              // first element of the array
}
PARAMETER PASSING BY VALUE:
void f( int A ) {
   A = 10;                // change the value of parameter A to 10
}

void g() {
   int B =  5;
   f(B);
                                              // B has not changed even though the function f changed the
                                              // value to 10
}