-1

Question : Is Java Pass By Value or Pass By Reference? This was the question asked in one of the interview.

Answer : In most of the Java books it is given that Java is pass by value. So I told this answer. But interviewer gave me an example of arraylist. Where we pass an arraylist to function() and in this function we remove an element and same is reflected back on original array. This means Java also supports pass by reference. I was confused so asking this Question to all of you.

Example of ArrayList :

public static void functionToRemoveAnElement(List<String>strs){
    strs.remove(0);

}   



  public static void main(String args[]) {
        List<String> strs = new ArrayList<String>();

        strs.add("nishant1");
        strs.add("nishant2");
        strs.add("nishant3");
        strs.add("nishant4");
        strs.add("nishant5");
        strs.add("nishant6");
        strs.add("nishant7");
                for(String str : strs){
            System.out.println("Before Calling Function : "+str); 
                    //here it will print all the values
        }

        functionToRemoveAnElement(strs);

                for(String str : strs){
            System.out.println("After Calling Function : "+str);
                //here it will not print nishant1 after calling function()
    }
}

This is O/P for those who are saying the values will not change:
Before Calling Function : nishant1

Before Calling Function : nishant2

Before Calling Function : nishant3

Before Calling Function : nishant4

Before Calling Function : nishant5

Before Calling Function : nishant6

Before Calling Function : nishant7


After Calling Function : nishant2

After Calling Function : nishant3

After Calling Function : nishant4

After Calling Function : nishant5

After Calling Function : nishant6

After Calling Function : nishant7

We can see that nishant1 is not present after the call as we are removing 1st element.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
NishantM
  • 121
  • 1
  • 6
  • 1
    Java is always pass by value. Always. (For reference types, a *reference value* is passed.) – user2864740 May 23 '14 at 07:00
  • 2
    Duplicate of http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Sanjeev May 23 '14 at 07:00
  • @NishantM, just read the answer to [this question](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) again and again until you get it. – Christian Tapia May 23 '14 at 07:01

2 Answers2

1

Java all ways Pass by value, In case of Object reference value will be copied to function paramter.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

Java is always pass-by-value. However, when you pass a reference to a function, you could alter the object that the reference points to. However, if you change the reference inside the function, that doesn't affect the caller.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • "change the reference" is why such terminology should be avoided! A variable is *not* a reference and a reference cannot be changed (just as 0 cannot be changed to a different number). – user2864740 May 23 '14 at 07:02
  • @user2864740 I would prefer to say pointer, since that's what references in Java are, but that seems to be a four-lettered word in the Java world. ;-) – C. K. Young May 23 '14 at 07:03
  • 1
    No, "pointer" is worse and is still wrong for the same reason. Rather, under Call by Value, *reassignments* to [parameter] variables have no effect on the callers bindings. Even under a reassignment, the reference (value) itself is never changed. – user2864740 May 23 '14 at 07:03
  • Eh, I'll just nuke my answer. It's clearly already well-answered elsewhere. :-D – C. K. Young May 23 '14 at 07:05
  • I'm just prodding for precise language! xD – user2864740 May 23 '14 at 07:06
  • @user2864740 what's wrong with "change the reference"? Could you explain what exactly do you mean by *reference*? – Christian Tapia May 23 '14 at 07:08
  • @Christian Java has a specific meaning for "reference", which is basically a pointer to an object instance. What user2864740 means is that references are themselves rvalues, whereas variables are lvalues that (if of the correct type) can hold rvalue references. – C. K. Young May 23 '14 at 07:14
  • In comparison, C++ has real pass-by-reference, and references in C++ are lvalues. – C. K. Young May 23 '14 at 07:15
  • @ChrisJester-Young So, the reference would be like a direction in memory? So you can't modify it, but you can make two variables to "point" to the same reference? – Christian Tapia May 23 '14 at 07:20
  • This is O/P for those who are saying the values will not change: – NishantM May 23 '14 at 10:13