r/javahelp • • 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

16 Upvotes

30 comments sorted by

View all comments

1

u/wildjokers May 15 '20

Java is pass-by-value and only pass-by-value.

http://www.javadude.com/articles/passbyvalue.htm

-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

u/[deleted] May 16 '20

[deleted]

-1

u/Jarl-67 May 17 '20

Your answer includes both always and sometimes ‘by value’. Which is it?

0

u/[deleted] 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/CarefulHideousBusinesssoftware

As 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#40523

https://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)