0

I'm interested to know exactly what's happening under the bonnet when passing a variable or object into a function.

When passing an object or variable into a function, is a new copy of the object/variable created in the new scope? (A set of parentheses constitutes a scope in java right?). Or is the reference to the existing variable/object in memory passed in? Although that would only make sense for a global object/variable?

Xerphiel
  • 1,053
  • 2
  • 12
  • 23

5 Answers5

3

java is always pass by value so a new variable or reference variable(which refer to some object) will be created in the function to receive the value that has passed to it... The scope of these variable will be withing that function in which it has created.

One thing you should know that even object are passed by value in java...when people say we pass the object to method ,that time we actually pass the value referred by reference variable not the object...so both the old and new reference variable refer to same object in heap memory..

check this for reference...

http://javadude.com/articles/passbyvalue.htm

http://www.programmerinterview.com/index.php/java-questions/does-java-pass-by-reference-or-by-value/

TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35
3

The easiest way to think of this is to get away from thinking of variables as ever being objects. A reference variable or expression is either null or a pointer to an object of appropriate class for its type.

Under this model, all Java argument passing is by value.

When you pass a reference to a method, you pass the null-or-pointer value to it. Assignment to the argument only affects the argument. It does not affect any variables in the caller's environment. On the other hand, if it is not null it points to the same object as the caller's variable or expression pointed to. Calling a value-changing method in that object changes its value for all code using a pointer to that object, including the caller.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
2

Both - you get a copy of the object reference (for objects), and a copy of the value for primitives.

So unlike C, you can't pass in a variable reference (for a string for example) and end up with it being repointed to something else. And you can't pass in an int, for example, and change it's value within the method - any changes it to it will only be within the method scope.

e.g:

MyObjectHolder holder = new MyObjectHolder();
holder.setObject(new Object());

//holder reference id = 1
// holder.object reference id = 2

doIt(holder);

public void doIt(MyObjectHolder methodScopeHolder) {


// methodScpeHolder reference id = 3
// methodScopeHolder.object reference id = 2

}
Aaron
  • 652
  • 4
  • 10
  • So the copy within the method scope, is a completely separate variable/object? – Xerphiel Sep 22 '13 at 12:47
  • 1
    @Xerphiel Yes and no. Its a completely separate variable, but that object reference points to the original object. So if you pass in a map, for example, if you add stuff to the map those items will be in the map after the method finishes. But if you set the map to null, when the method returns the original variable will still point to the map. – Aaron Sep 22 '13 at 12:51
  • 2
    @Xerphiel So for example, if you have a key to a car, and make me a copy, i can get into the car and do whatever i want to it. I can't, however, change your copy of the key. So you don't have to worry that after I'm done your key will now open a different car by mistake. – Aaron Sep 22 '13 at 12:54
  • 1
    So when you say, setting the map to null, you really mean setting the reference to the object to null? You can add to the map instance, and you can render your reference to the map null, but outside the method the original reference to the map still exists? – Xerphiel Sep 22 '13 at 13:01
  • 1
    Exactly. Sounds like you're 'getting' it. Remember too that some objects are immutable (like string), so you can't change their innards at all (when you modify the string you get a new object). Don't get confused by that. – Aaron Sep 22 '13 at 13:14
  • Yeah, i came across the String being immutable, was initially confusing. Thanks for the info. To clarify with the variables. You get a copy of the variable, references within the method are pointing to the copy of the variable, so any changes are only within the scope? Test code seems to agree with that. – Xerphiel Sep 22 '13 at 13:22
  • Yeah. The method parameters are method scope only, so if you point them somewhere else you don't change the original reference outside of the method. If you change stuff within the object that reference points to, then that does last outside the method scope. You can check it out by printing the object references of the parameter before passing it in, and again inside the method - they'll be different. Even better - use a breakpoint and look in the debugger. You'll see two different object Ids, but the ids of objects inside those references (eg map values) will be common. – Aaron Sep 22 '13 at 13:33
  • 1
    @Xerphiel I added an example to my answer to demonstrate. – Aaron Sep 22 '13 at 13:39
1

In Java your program's "local" variables are maintained in a "stack frame", which is a section of a large array whose elements can contain any data type.

When you call, you copy the parameters (which may be either "scalars" -- chars, ints, floats, etc -- or "references") into a new area of the array (the "top"). Then, during the call, the index values that control which elements of the array you can access are adjusted, and the copied parameters become the "bottom" of a new stack frame, with the called method's local variables being above parameters. So to the new method its copies of the parameters are just like local variables.

Effectively, each method has a "window" into the overall stack, and the "windows" overlap to cover the parameter list.

Of course, when you "pass" an object you're really just passing a reference to the object, and the object itself is not copied.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
-1

When you pass a variable, you are passing the reference. When you pass an object, you are passing a copy of it.

Palash
  • 498
  • 4
  • 12
  • 1
    It's exactly the other way round (objects are passed by reference) –  Sep 22 '13 at 12:48
  • 1
    Please give example as I do not agree with any of you...As far I know java is always pass by value it does not matter whether we are passing object(reference variable) or variable – TheGraduateGuy Sep 22 '13 at 12:51
  • What I am trying to say is that, creating an object is like creating a new box, copying all the functions and stuff of the class there and giving access to the box to whatever is calling for the object. – Palash Sep 23 '13 at 07:29