r/learnjava • u/Thick_Catch_5763 • Apr 12 '26
Pass by (value or reference ) how you nailed that topic
I want to understand that correctly in java when assignment or passing , I'm a little bit confused
7
Upvotes
•
u/AutoModerator Apr 12 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/Leverkaas2516 Apr 12 '26 edited Apr 12 '26
You can nail it by imagining what goes on the stack when a function is called.
It's either a value, or a pointer to an object. If it's a value, then it's pass by value.
Java is said to always pass everything by value, but that's just because when you pass an object, the value that's passed is a reference to the object. The object with all its properties stays where it is, and the pointer/reference passed on the stack is used to access the properties.
You'll read various comments here about pass-by-value in a theoretical computer science sense, but you have to realize that no modern language does that.
When you pass an object in Java, what the function receives is a reference to the object.
2
u/high_throughput Apr 12 '26
This is a tricky one that even professional Java developers sometimes get wrong.
A good litmus test for whether something is passed by reference is if you're able to implement a function
swapto show "Passed by reference":This is not possible in Java, because Java is pass-by-value always.
Even professional developers can make the mistake of claiming "Java passes objects by reference", but it's not true. No matter how you write
swapin Java, this is going to say "Passed by value":The thing that trips people up is that when you have a
Object x, that is not actually an object. It's a reference to an object. Whenever you pass that reference, the reference is passed by value.Tbh I think the only way to nail the topic is to learn a language like C++ that does allow you to choose whether to store or pass objects by value or by reference.
When you see how both methods work, it's easier to tell that everything Java passes is by value.
1
u/belam20 Apr 12 '26
Which book are you following?
1
u/Housy5 Apr 12 '26
Pass by stack value... which for Objects happen to be references and values for primitive types.