Home > Uncategorized > Parameter Passing in Java

Parameter Passing in Java

The way that java actually handles the passing of parameters in methods is something that a lot of people are confused by. Since I’ve already been down this route, I thought that I’d lay some markers to help others who could benefit from this knowledge.

Java has much in common with C++, but the lack of pointers can complicate things. You don’t actually lose any of the power of pointers though. This is because all parameters are passed by reference (remember prepending the & sign in C++?)

Note: This post is a follow-on from the previous one that discussed the proper way to remove elements from Java Collections when a live iterator is present.

Consider the code snippet given below:

import java.util.*;

public static void main(String argv[]) {
Hashtable ht = new Hashtable();
ht.put(1, “Curly”);
ht.put(2, “Larry”);

public static void first(Hashtable ht_inside) {
ht_inside.put(3, “Moe”);

public static void second(Hashtable ht_inside) {
ht_inside = new Hashtable();
ht_inside.put(3, “Leo”);

Before first() is called, ht contains ‘Curly’ and ‘Larry’.

After first() is called, ht contains ‘Curly’, ‘Larry’ and ‘Moe’. Note that this change has not only taken place in the local ht in the scope of first(), but also in the ht in the scope of main().

when second() is called, only the ht in second is modified (to contain ‘Leo’). This is because this local ht has been newly created and initiated, and is in a different memory spot as compared to the ht in first() which was passed by reference.

Thus local changes made to ht in second() do not effect the ht in main(), while local changes made to ht in first()always change the ht in main() as it was the same ht that was passed by reference.

  1. fazel
    October 15th, 2009 at 02:00 | #1

    Good point Shahzad…
    But remember, Java has this confusion only with Collections such as Map, Hashes(table,map,…)… Java says: Hashtable is synchronized. It might cause the problem.
    So, in first() if you pass ht.clone, then first() won’t affect the main()..

  2. October 15th, 2009 at 10:45 | #2

    The whole point of passing by reference it that you want to modify the ht in main().

    The place where this passing by reference really shine is when you need to modify/generate more than one parameter though a method. Remember that you can only return a single parameter from a method. To overcome this limitation, you’d want to create the Collection object in the main method, and you can then send this same object as a parameter in more than one method in order to process them in a pipelined manner.

    This is especially important in NLP where you have many different operations on the same data i.e. sentence segmentations, tokenization, part-of-speech etc etc.

    This allows you to create truly enterprise grade and maintainable software.

    As a bonus, it is also much more faster and memory efficient, as the alternative of creating clones takes both space and CPU cycles.

    Obviously, there are benefits to cloning (primarily as it is non-destructive operator), but for the applications I identified in this response, passing by reference (and not employing cloning) is more appropriate.

  3. fazel
    October 15th, 2009 at 17:11 | #3

    thanks Shahzad,

    Suppose I need to take an instance of ht and pass it to first(), and do whatever I need to do: add, remove,… But I want to keep the original ht in main() for rainy day! (i.e in another method, I need my original ht to do something else ..)
    It was my problem that I used ht.clone
    Any idea?


  1. No trackbacks yet.