The world’s most dreaded Java interview question

One of the interview questions I have always hated is “does Java use call-by-reference or call-by-value”?

This is a perfect question to trip up a developer like myself who started out with C, then spent years writing solid Java code without ever really thinking much about this issue.

If you google around a bit, you’ll find many attempts at answering this question.

Personally, I think this is a trick question. The terms “call by reference” and “call by value” have no real meaning in Java, because Java lacks the semantics to control whether one is dealing with a “reference” (i.e. a memory address) or a “value” (the content of memory at that address). I would argue that those concepts don’t really apply to Java, because Java has no pointers. But of course, somebody will probably read this and point out that James Gosling himself states Java is always pass-by-value, and object references are just “reference values”. This is pretty darn confusing, to say the least.

The important thing to understand is how Java operates on method arguments. So I’ve created this little code example which demonstrates a key point, which tripped me up in a recent interview. Take a look at the code and see if you can tell what will be printed out when it runs:


public class JavaTest
{
    private String _name;

    public JavaTest(String s)  {
        _name = s;
    }

    public void setName(String _name)
    {
        this._name = _name;
    }

    public String toString() {
        return _name;
    }

    public static void changeMe(JavaTest t) {
        t = new JavaTest("bar");
        System.out.println("inside changeMe, t is " + t);        
    }

    public static void changeName(JavaTest t) {
        t.setName("bar");   
Using massage techniques for acute prostatitis can actually make the situation worse. molineanimalaid.org online prescription for cialis Erectile dysfunction can be cause by many reasons like overwork, stress, depressions (20% of cases), and more often (80%) is related to previous health issues. http://www.molineanimalaid.org/viagra-8111 viagra tablets online It must be noted that Arginine may promote results that may further the adversity of side effects of some other medicines.  pill sildenafil Over time, this can lead to nerve damage in kidneys, tadalafil tablets 20mg  heart and eyes.     }

    public static void main (String args[]) {
        JavaTest myObj = new JavaTest("foo");
        System.out.println("myObj: " + myObj);
        changeMe(myObj);
        System.out.println("after calling changeMe, myObj is still: " + myObj);
        changeName(myObj);
        System.out.println("after calling changeName, myObj is now: " + myObj);
    }

}

Here’s the output:

myObj: foo
inside changeMe, t is bar
after calling changeMe, myObj is: foo
after calling changeName, myObj is: bar

Note that the method changeMe() doesn’t actually change myObj, even though it assigns the variable t to a new instance of JavaTest. The variable t in this method starts out holding a reference to myObj, but it gets replaced with a local reference to a new instance of JavaTest. Meanwhile, back in the calling context, myObj remains unchanged.

In changeName() however, no such replacement happens, so the name change gets applied to myObj which is “referenced” by the variable t.

I hope this helps somebody avoid falling prey to this well-worn interview “gotcha”.

Leave a Reply

Your email address will not be published. Required fields are marked *