Some things that i would like to point out. Java is a pass by value language. Not a pass by refrence. First let me explain pass by refrence. When a variable is passed to a method a copy of that variable is passed. If the method modifies it's parameters those modifications are visible when the method returns.
Java does not do this. It is a pass by value language. For instance. The following code illustrates this point.
Code:
class Example{
public static void main(String[] args){
int i = 10;
printInt(i);
System.out.println(i); // prints 10
}
static void printInt(int i){
System.out.println(i); // prints 10
i = 25;
}
}
However when a refrence type is involved, the valus is passed by refrence. This is not the same as "pass-by-refrence". If java were a pass by refrence language, when a refrence type was passed to a method, it would be passed as a refrence to the refrence.
Heres another example. Even though we are modifying the contents of the variable i and returning that value from the method the contents of the variable were never changed.
Code:
class Example{
public static void main(String[] args){
int i = 10;
int k = 0;
k = printInt(i);
System.out.println(k); // prints 10
}
static int printInt(int i){
System.out.println(i); // prints 10
i = 25;
return i;
}
}