r/javahelp • u/djpfg • May 15 '20
how reference variables are passed as method arguments in Java
Hello, I am struggling to understand what this question is asking for, I know in C++ for pass by reference the address of the variable is passed and altered before returning unlike pass-by value(correct me if Im wrong) but in java I thought all values were automatically pass by reference? struggling to make sense of the question so any help at all will be greatly appreciated:)
Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java. A good answer will include a simple example of each
13
u/Yithar Intermediate Brewer May 15 '20
Java is always pass by value. It just so happens that what is in obj when you say Object obj = new Object(); is a memory address. So what happens is a method given obj is given a copy of the memory address.
The reason you know it's at least sometimes pass by value is because of how things work with primitives. If it was pass by reference, modifying an int passed to a method should modify the original one as well.
In C++, passing a pointer by reference means you can actually modify the original memory address from the method itself and the change will be reflected when the function returns. You can not do this in Java.
https://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c
1
4
u/feral_claire Software Dev May 15 '20
Java is always pass by value. When working with objects, passing an object to a method is similar to passing a pointer by value in c++. Java does not have pass by reference like c++ does.
-5
u/Jarl-67 May 15 '20
This is simply incorrect.
3
u/deltageek Extreme Brewer May 15 '20
Apart from not being able to do silly things like pointer arithmetic, that explanation is exactly correct
3
u/iamsooldithurts May 15 '20
Java always passes parameters by value; the method receives a copy of the value given. The difference is whether what is being passed is a primitive or String, or an object.
Primitives hold the value assigned.
Int i = 1;
int j = 1;
i == j evaluates to trueStrings point to a static spot in memory where the string is stored.
String s = “hello”;
String t = “hello”;
s == t evaluates to true because the compiler creates the string in memory and assigns the address to both variables.Objects hold an address in memory.
Integer i = new Integer(1);
Integer j = new Integer(1);
i == j evaluates to false because the addresses are different. However, if you do i = j then i == j will evaluate to true because they both reference the same address in memory2
1
u/wildjokers May 15 '20
Java is pass-by-value and only pass-by-value.
-1
u/Jarl-67 May 16 '20
Except if you pass an object as a parameter....
1
u/wildjokers May 16 '20
I will reiterate, java is pass-by-value and only pass-by-value. Did you read the article I linked to?
0
May 16 '20
[deleted]
-1
u/Jarl-67 May 17 '20
Your answer includes both always and sometimes ‘by value’. Which is it?
0
May 17 '20
[deleted]
-1
u/Jarl-67 May 17 '20 edited May 17 '20
it's at least sometimes pass by value
Your quotes inlude 'Java is always pass by value', ' it's at least sometimes pass by value', 'It's always.'. Those are your quotes and they contradict each other.
Java pass by reference example
For parameters that are pass by value, it's impossible to change a parameter where that change lasts past the method execution. In this simple example, you can see that an object is modified and that modification persists after the method is complete. This situation is impossible in a pass by value scenario.
Running the example yields this result:
Before method: Joe "by Value" Java
Start method: Joe "by Value" Java
End method: Joe "by Reference" Java
After method: Joe "by Reference" Java
2
u/random740f May 17 '20
In case anyone stumbles upon this thread in the future:
That is still pass by value... Like I said go back and study your Java fundamentals.
I never said you could not modify the internal fields within the object itself. I said modify the actual memory address. What you are saying is you are an amateur (I actually work in the industry as a professional) who does not understand what "pass by reference" actually means.
Show me how to modify the memory address of a variable in the caller from the called method itself:
https://repl.it/repls/CarefulHideousBusinesssoftwareAs I said, you are 100% wrong because that C++ code is impossible in Java.
Clearly everyone in this StackOverflow thread is wrong and you're right:
https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/40523#40523https://www.reddit.com/r/javahelp/comments/9gkzt6/why_does_java_pass_parameters_by_value_instead_of/
What would you need pass by reference for? Java doesn’t have C/C++ pointers. Object references are passed by value, which still allows you to update the internals of an object.
Did you even read the OP?
Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java.
That question would not make sense if pass-by-reference existed in Java.
This disproves that false narrative. Examples given include primitives and the reference itself but that is only partially pass by value.
Nope. Basically you are telling me that you are an amateur and you have no idea what pass by reference actually means.
As I said, I think you are one of those people that can not just admit when they are wrong. :)
0
u/Jarl-67 May 17 '20
You are the one who can’t admit that you are wrong which is why your own descriptions contradict each other. Always and sometimes have very different meanings but you seem a bit short on semantics.
Here is a great blog post on reality: pass by reference
1
u/wildjokers May 18 '20 edited May 18 '20
Are you just trolling now or do you really believe java is pass-by-reference?
Java is pass-by-value and only pass-by-value. You can change the internal state of an object in a method and that change will be reflected outside of the method. That is fine. The original reference and your copy of it still point at the same object.
However, if you change the value of the parameter that was passed into your method, by calling
new Foo(), that change will not be reflected outside of the method.new Foo()returns a new object reference and now your parameter has a different value from the passed in reference value.1
u/Jarl-67 May 18 '20
It isn’t pass by value because you say so. Logic says otherwise. The parameter is an object, and it’s the entire object that needs to be by value. The only reason you think it’s by value is that that the actual reference (key word) is not changeable. When a parameter is by value, no part of the parameter is changeable.
Didn’t you learn this in college? I assume you have a degree but maybe that’s too big of an assumption.
This isn’t a belief by the way, you are the one who believes. That’s why you can’t actually argue the point but instead just restate your untenable position.
→ More replies (0)
-1
u/FurkinLurkin May 15 '20 edited May 15 '20
Removed so I don't muck up the real answer
1
u/djpfg May 15 '20
so say you pass a int x=4 into a square(int x) method, the value of x before being passed is stored in file location 0xFF04, when the value returns as 16 the pointer to x will move to file location 0xFF08 and 16 will be stored there and that is what will show in the code?
Thank you very much for your response, I have my final exam tomorrow and this question freaked me out
3
u/FurkinLurkin May 15 '20
Sorry for confusing the subject. After reading that javadude.com linked article in someone else's answer I realized I had the definitions of what a reference and what a value is swapped. The article is a pretty good explanation and has the proper examples that I was fumbling to give.
5
u/GuyWithLag May 15 '20
Java does not have pass-by-reference.
Given a local variable declaration
Foo x = ...;, there is no way for a method that takesxas a parameter to change the value ofxoutside its scope.Java is strictly pass-by-value; for object references the reference to the object is passed as a value.
3
-1
u/Jarl-67 May 17 '20
There are a lot of definitive "Java is pass by value only" statements in the comments. This disproves that false narrative. Examples given include primitives and the reference itself but that is only partially pass by value. Java passes the pointer by value and that's it. The object is modifiable as the reference to that object is the same. No copy of an object parameter is made in java.
Java pass by reference example
For parameters that are pass by value, it's impossible to change a parameter where that change lasts past the method execution. In this simple example, you can see that an object is modified and that modification persists after the method is complete. This situation is impossible in a pass by value scenario.
Running the example yields this result:
Before method: Joe "by Value" Java
Start method: Joe "by Value" Java
End method: Joe "by Reference" Java
After method: Joe "by Reference" Java
1
u/random740f May 17 '20
In case anyone stumbles upon this thread in the future:
That is still pass by value... Like I said go back and study your Java fundamentals.
I never said you could not modify the internal fields within the object itself. I said modify the actual memory address. What you are saying is you are an amateur (I actually work in the industry as a professional) who does not understand what "pass by reference" actually means.
Show me how to modify the memory address of a variable in the caller from the called method itself:
https://repl.it/repls/CarefulHideousBusinesssoftwareAs I said, you are 100% wrong because that C++ code is impossible in Java.
Clearly everyone in this StackOverflow thread is wrong and you're right:
https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/40523#40523https://www.reddit.com/r/javahelp/comments/9gkzt6/why_does_java_pass_parameters_by_value_instead_of/
What would you need pass by reference for? Java doesn’t have C/C++ pointers. Object references are passed by value, which still allows you to update the internals of an object.
Did you even read the OP?
Explain the difference between how pass-by-reference operates in C++ and how reference variables are passed as method arguments in Java.
That question would not make sense if pass-by-reference existed in Java.
This disproves that false narrative. Examples given include primitives and the reference itself but that is only partially pass by value.
Nope. Basically you are telling me that you are an amateur and you have no idea what pass by reference actually means.
As I said, I think you are one of those people that can not just admit when they are wrong. :)
1
u/Jarl-67 May 17 '20
"Nope." because you said so? That's your argument? Like I said, it's impossible to modify a Pass by value parameter and that is true in any language. Cleary you are able to do that in java. References and pointers are the same thing. Only the reference/pointer in java is pass by value.
7
u/firsthour Profressional developer since 2006 May 15 '20
Why don't you tell us what you know so far.