Java, pass-by-value, reference variables

There’s an extended discussion of how Java passes variables at an earlier question “Is Java pass by reference?”. Java really passes object references by value.

In your code, the references for the arrays (which are objects) are passed into calculate(). These references are passed by value, which means that any changes to the values of b and c are visible only within the method (they are really just a copy of s_ccc and t_ccc). That’s why t_ccc in main() was never affected.

To reinforce this concept, some programmers declare the method parameters as final variables:

public static void calculate(final int[] b, final int[] c) 

Now, the compiler won’t even allow you to change the values of b or c. Of course, the downside of this is that you can no longer manipulate them conveniently within your method.

Leave a Comment