You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
-
4It's still pass-by-value even if the value itself is a reference. The argument is copied and reassigning it doesn't affect anything outside the called function - this is the key difference between pass-by-value/reference.Michael– Michael2021-09-21 04:50:28 +00:00Commented Sep 21, 2021 at 4:50
-
2This seems like the correct answer to me. Why are people so caught up on the technicalities of pass-by-object-reference versus pass-by-reference? If you pass a non-primitive variable to a function, modifications to that variable within the function affect the variable outside of the function. That is pretty much the same behavior as pass-by-reference. "It's still pass-by-value even if the value itself is a reference" - umm, okay, but if the value is a reference then you are passing a reference. Terminology aside, passing objects as arguments can affect the object outside of the function.h0r53– h0r532021-10-19 16:09:13 +00:00Commented Oct 19, 2021 at 16:09
-
2@h0r53 Why are people so caught up? Consistent definitions are import for effective communication. What C does, IS ALSO PASS BY VALUE. It's just the java calls its pointers "References". And that's where the confusion comes from. It's a different use of the word reference than in "pass-by-reference". Under the hood a "reference" in java is just a pointer, meaning it's a primitive which holds an address to the object in memory. Yes, you change the object in memory, but not the reference itself. So if you assign a different object or set it to null, the reference outside the function wont changeSanjeev– Sanjeev2022-02-19 17:08:35 +00:00Commented Feb 19, 2022 at 17:08
-
1@h0r53, I can see from your posts (which are great, btw) that you know C and C++. C does not offer Pass by Reference, but C++ actually does by way of allowing "reference parameters". When you declare a function with a header like , void foo(TreeClass& maple), the ampersand makes this a reference parameter (can't do that in C). Now, if you set maple to null inside the function, the change will also be reflected outside the method. Please see my post for details stackoverflow.com/questions/40480/…Sanjeev– Sanjeev2022-02-19 17:24:00 +00:00Commented Feb 19, 2022 at 17:24
-
1@Sanjeev I see your point and the subtle differences in reference v. object-reference are more clear to me. I was admittedly a bit frustrated when writing the prior response because it seemed like the issue was being unnecessarily convoluted with terminology. I just wanted a quick answer - "If I pass an object to a function, and change that object within the function, is it reflected in the caller?" My background causes me to think of everything in terms of pointers, so when I think of pass-by-reference or pass-by-object-reference, I think of pass-by-pointer, which isn't technically correct.h0r53– h0r532022-02-22 16:13:53 +00:00Commented Feb 22, 2022 at 16:13
|
Show 3 more comments
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
-
create code fences with backticks ` or tildes ~
```
like so
``` -
add language identifier to highlight code
```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible)
<https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. python-3.x), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-java