P
pek
OK.. Assuming that pass-by-reference means that the following code
would work:
public void swap(Human h) {
h = new Human();
}
Which everybody knows, it doesn't in Java. I'm guessing this works
with C++. Even if it doesn't in C++ (I wouldn't know), I'll be using C+
+ as a language that passes by reference for the sake of my questions.
Now, I know Java passes object references by value, but how exactly
does it do it in memory?
Correct me if I'm wrong anywhere. This is what I think. Suppose we
have the following code:
....
Human h; // (1)
h = new Human(); // (2)
change(h);
....
public void change(Human o) { // (3)
o = new Human(); // (4)
}
In Java:
1. Allocates a small memory block which will be used to point to an
object of type Human
2. Allocates memory for the newly create Human object and changes the
value of the memory block of reference h (which was previously null)
to point to the new Human
3. When called, it allocates memory for the local reference variable o
and copies the pointer of the passed reference, thus, both the called
and the local point at the same object
4. Allocates memory for the newly created Human object and changes the
pointer of the local reference o to it, thus, any changes to o don't
affect the passed reference (h).
So, right before the method change() ends, the memory would allocate:
two memory blocks for each object (the one from main and the one from
change) and two reference variables that have pointers that point to
each object.
Because I don't know C++, I'll use the code from Java and talk about
memory allocation as I think. So, in C++:
1. Nothing happens, I don't know if this actually works. I don't know
how pointers work in memory.
2. Again, memory is allocated for the newly created object and now h
is a pointer pointing at the memory block.
3. When called, the memory location of the passed pointer will copied
to o, thus, both pointing at the same object
4. ??? What happens here???? h and o are both pointers that point to
the same object. If I change o to point to another object, how does h
know about it? What about the previous object?
So, right before change() ends the memory allocates: two objects for h
and o (with no pointers at h, and now it must be garbage
collected....which won't) and....what? Two pointers? Does a pointer
allocate memory?
Am I right about the memory allocations in the Java code? What about
pointers in C++? Do they allocate any memory space? If they don't, how
does it store the pointers memory location? What are pointers in terms
of memory?
I hope I made my questions as clear as possible. Unfortunately, I
can't post an image to illustrate my point. I'm trying to create a
slide about Pass-by-value, Pass-by-reference and Pass-reference-by-
value. So I need this information in order to create a good
illustration of the concepts (which unfortunately I didn't find
anywhere on the internet).
I would appreciate any comments, suggestions, websites... Anything
actually. Thank you very much for taking time to read this and for any
help in advance.
Regards,
Panagiotis
would work:
public void swap(Human h) {
h = new Human();
}
Which everybody knows, it doesn't in Java. I'm guessing this works
with C++. Even if it doesn't in C++ (I wouldn't know), I'll be using C+
+ as a language that passes by reference for the sake of my questions.
Now, I know Java passes object references by value, but how exactly
does it do it in memory?
Correct me if I'm wrong anywhere. This is what I think. Suppose we
have the following code:
....
Human h; // (1)
h = new Human(); // (2)
change(h);
....
public void change(Human o) { // (3)
o = new Human(); // (4)
}
In Java:
1. Allocates a small memory block which will be used to point to an
object of type Human
2. Allocates memory for the newly create Human object and changes the
value of the memory block of reference h (which was previously null)
to point to the new Human
3. When called, it allocates memory for the local reference variable o
and copies the pointer of the passed reference, thus, both the called
and the local point at the same object
4. Allocates memory for the newly created Human object and changes the
pointer of the local reference o to it, thus, any changes to o don't
affect the passed reference (h).
So, right before the method change() ends, the memory would allocate:
two memory blocks for each object (the one from main and the one from
change) and two reference variables that have pointers that point to
each object.
Because I don't know C++, I'll use the code from Java and talk about
memory allocation as I think. So, in C++:
1. Nothing happens, I don't know if this actually works. I don't know
how pointers work in memory.
2. Again, memory is allocated for the newly created object and now h
is a pointer pointing at the memory block.
3. When called, the memory location of the passed pointer will copied
to o, thus, both pointing at the same object
4. ??? What happens here???? h and o are both pointers that point to
the same object. If I change o to point to another object, how does h
know about it? What about the previous object?
So, right before change() ends the memory allocates: two objects for h
and o (with no pointers at h, and now it must be garbage
collected....which won't) and....what? Two pointers? Does a pointer
allocate memory?
Am I right about the memory allocations in the Java code? What about
pointers in C++? Do they allocate any memory space? If they don't, how
does it store the pointers memory location? What are pointers in terms
of memory?
I hope I made my questions as clear as possible. Unfortunately, I
can't post an image to illustrate my point. I'm trying to create a
slide about Pass-by-value, Pass-by-reference and Pass-reference-by-
value. So I need this information in order to create a good
illustration of the concepts (which unfortunately I didn't find
anywhere on the internet).
I would appreciate any comments, suggestions, websites... Anything
actually. Thank you very much for taking time to read this and for any
help in advance.
Regards,
Panagiotis