0

I need a CS kind of explanation on how the following 2 methods work differently from each other. I do not have a CS background (studied accounting .. blaahh) so please elaborate where you think is necessary. So java is a pass by value language from what I've read.

Consider the below primitive method. What exactly is it going to do in the background? The bit equivalence of lets say int myvar =7; will be made a copy of and passed into the method? How, please elaborate?

public void changePrim(int var){
    var = 100;
    System.out.println("in change prim:"+ var);
}

Next, the below method is going to have a variable passed into it that will be of type Person.

public void changePersonObject(Person p) {
    p.setAge(100);
    p.setFirstName("Lost");
    p.setLastName("Boy");
    //   p = new Person("Lost", "Boy", 24, 100);  // this creates new. why?
}

So the first primitive argument method didn't change the int variable that was passed in to it from the client. Why does the second butcher the Person object's first and last name? Please elaborate with some emphasis in Computer Science. I'm learning that in parallel along with how java works. So bits and bytes would be deliciously devoured. Thank you.

Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • I'm not going to write an article explaining how stacks work, but the basic answer to what you're getting at is that Java passes values to methods, and those values show up as local variables inside the methods. If the value is a reference to an object, replacing the local variable (`p = new Person();`) won't affect the object passed in, but *using* the reference value to locate an object and change it will change the same object that the calling code is looking at. – chrylis -cautiouslyoptimistic- Dec 30 '13 at 04:07
  • No this is not a duplicate. i need explanation in CS. I need the bits and bytes. What and how is something going on the stack and heap! – Horse Voice Dec 30 '13 at 04:23
  • Maybe this [Stack Overflow Convo](http://stackoverflow.com/questions/19623563/where-does-java-reference-variable-stored) will get you a little bit closer. – Code-Junkie Dec 30 '13 at 04:29
  • If this is object - it stored on heap, primitive type stored on stack. When object stored on heap, its reference stored on stack. So that when you call that method with parameter of object type passed by value, it is the reference copied and passed, not the object copied and passed. This is it – T.S. Dec 30 '13 at 05:13

1 Answers1

0

It's meaningless to talk about bits and bytes as that depends on the JVM implementation, not on the Java language. This is how the Java Language Specification talks about it.

Say you have

int a = 10;

in some method. Somewhere in the stack frame, there's a map storing the local variable a and its value 10. Say you then invoke

obj.changePrim(a);

That map, of variables to values, will be accessed to retrieve the value for the variable a. A new stack frame will be pushed on the stack for the changePrim method. This invocation will cause a new variable, the parameter var to be pushed into this new stack frame's map of variables/values with the value retrieved earlier. This and this might help you understand what this map and these values are.

Looking at the javadoc of Value, you should see that a sub type of Value is ObjectReference. So the value of a variable of a reference type, eg. Person, is just the value of the reference to the object. It works exactly the same way as I described above.


The question marked as duplicate explains your misunderstanding with the reference type, but here goes again in the terms I was using to explain it.

In

obj.changePersonObject(somePersonVar);

The code is executing in a stack frame where there is a variable somePersonVar that is mapped to a value that is an object reference. When you invoke your method

public void changePersonObject(Person p) {
    p.setAge(100);
    p.setFirstName("Lost");
    p.setLastName("Boy");
    //   p = new Person("Lost", "Boy", 24, 100);  // this creates new. why?
}

that object reference value is retrieved and assigned to the new stack frame's map as a value to the key for variable p. When you do

p.setAge(100);

The value for variable p is retrieved from the map. This gives you an object reference. To invoke the setAge() method, Java dereferences that value so that it can access the underlying object and call its method.

If instead you did

 p = new Person("Lost", "Boy", 24, 100);  // this creates new. why?

the map entry for p which was

 p ==> some object reference 

gets overwritten with the new value

 p ==> some new object reference

That doesn't affect the map from the previous stack frame.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • So the first primitive argument method didn't change the int variable that was passed in to it from the client. Why does the method that was passed a reference variable butcher the Person object's first and last name? – Horse Voice Dec 30 '13 at 13:40
  • @taz Take a look at my edit, let me know if something is unclear. – Sotirios Delimanolis Dec 30 '13 at 16:23