vayne nick wrote:Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners
Junilu Lacar wrote:Saying "when we pass the value of an object, we are passing the reference to it" isn't exactly the epitome of clarity either, especially in the context of differentiating between a "value" vs. a "reference". I believe one of the motivations for using the word "reference" was to differentiate it from the term "pointer" and what that implies for folks who have programmed in languages like C and C++. The main difference between a Java reference and a C-like pointer is that you can't perform arithmetic on a reference as you can with a pointer. You can't increment a reference to point to a different location in memory.
Welcome to the Ranch againvayne nick wrote:The Java Spec says that everything in Java is pass-by-value . . . .
I am afraid I think that tutorial is poor quality and not clear. It shows the two names not changing but doesn't explain how that phenomenon would differ between pass‑by‑value and pass‑by‑reference.Check t his example...java pass by value
Nick
Ryan McGuire wrote:I still fail to see the difference between passing an object by reference and passing an object reference by value. In both cases aren't you just passing a reference to an object?
Campbell Ritchie wrote:In the case of pass‑by‑value, line 3 will print xyz because the assignment in line 6 is applied to something distant.
In the case of pass‑by‑reference, line 3 will print pqr because the assignment in line 6 is applied to the same thing.
Neither Java® nor C supports pass‑by‑reference, but it is possible to mimic pass‑by‑reference in C.
If given the appropriate form of instructions, C++ supports pass‑by‑reference.
Junilu Lacar wrote:
Ryan McGuire wrote:I still fail to see the difference between passing an object by reference and passing an object reference by value. In both cases aren't you just passing a reference to an object?
The difference is that the actual parameter doesn't get changed when passing by value whereas it does when passing by reference. For example:
After the call to foo(bar) is made on line 7, the bar variable declared on line 6 would reference the Bar object created on line 2 if Java did pass-by-reference. Since this does not happen in Java because all parameters are passed by value instead, the bar variable will still reference the Bar object created on line 6 after the method call on line 7. The fact that you can alter the Bar object created on line 6 via the formal parameter b of method foo() is irrelevant to pass-by-reference vs. pass-by-value semantics.
In short, pass-by-reference means that any change to the formal parameter affects the actual parameter. When passing object references, the parameter is the reference, not the object being referenced.
Ryan McGuire wrote:Just reiterating the rule that Java is pass by-value only does not answer the question. What is the difference between passing a reference to an object and passing an object by reference? What can you do in either case that you can't in the other?
Ryan McGuire wrote:
1. Doesn't that change the object that was passed to foo()?
Henry Wong wrote:In Java, changing the reference variable has no affect on the caller.
That object is mutable, and you can change its state, so it changes from Banana to Mango. It is the same object, but its contents can be changed. That is different (as you already know) from exchanging one object for another.Ryan McGuire wrote:. . . This will print "Mango". This is because a reference to the object created in line 1 is being passed tot he method, right? So if the method is passed a reference to the object, why is it correct to say that the object reference is being passed by-value but NOT that the object is being passed by=reference?
I see that sort of problem all the time; people don't learn the jargon. Jargon can be confusing to start with, but if used correctly it is very precise and concise. The difference between passing a reference and by reference is significant. It might be better to say you are passing a copy of the reference.Just reiterating the rule that Java is pass by-value only does not answer the question. What is the difference between passing a reference to an object and passing an object by reference? What can you do in either case that you can't in the other?
Campbell Ritchie wrote:What you are doing is putting a number onto the stack representing the location of your banana. That is the reference to ...
Ryan McGuire wrote:
Is that all reasonable?
Ryan McGuire wrote:Is that all reasonable?
Liutauras Vilda wrote:Dmytro Grytskiv, welcome to the Ranch.
Fabulous indentation and formatting
You missed one case with List's reference variable re-assignment, which I think really needs to be in that example. What happens if line 31 you set to null? var = null;
Your called type as primary, I think would be better if were called as primitive type
So, Ryan, yours case and Henry's situations are different. You are changing objects state, while Henry saying, that changing reference variable has no effect. Reference variable in your case is 'b'. So you don't do anything with 'b', but rather with object, to which 'b' is refering to.
hen what you are doing is change that memory location. You are replacing the location of the mango with a different location which is papaya. But (if we use pass‑by‑value) all other numbers representing the mango still point to mango. Pass‑by‑value means the change of the value on the stack is restricted to one place: line 9. It means the a in line 3 does not reflect that changes. So line 4 prints mango.
Pass‑by‑reference would mean that the change in line 9 would be reflected in line 3 and the value of the memory location would change in two places; line 4 would print papaya. Pass‑by‑value means any changes to whichever reference you are using are confined to line 9.
It is not a problem about English; the terms pass by reference and pass by value hve specific computer science meanings which are not phrases used in ordinary English. People who have spoken English all their lives still get it wrong.Greg Sully wrote:. . . people like me that the native language is not English.
I think you are referring to this post.. . . Campbell explain this if possible again
. . .
I cant understand why print mango instead of papaya ?
. . .
With pass by reference, that would beA few minutes ago, I wrote:. . . Because Java® uses pass by value only, a is unchanged.
sc→object2→Papaya
a→object1→Mango
. . .
All things are lawful, but not all things are profitable.
Knute Snortum wrote:What you've written looks correct to me. And you executed the program to see if the results are what you predicted?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |