Java is "always pass by value" - utterly false nonsense

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 3171 Views - Last Post: 27 August 2017 - 04:19 AM

#16 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 19,861
  • Joined: 05-May 12

Re: Java is "always pass by value" - utterly false nonsense

Posted 28 June 2017 - 07:10 PM

I throw this out there and then duck... :)

So if it is pass by value, if I pass a 200 element array into a function, then that means another 200 element array will created for the use of the function? How will sorting work?
Was This Post Helpful? 0
  • +
  • -

#17 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-December 12

Re: Java is "always pass by value" - utterly false nonsense

Posted 07 August 2017 - 07:51 AM

Finally I found a bit of common sense in some Oracle documentation:

Although Java is quite similar to C in its syntax, it does not support direct pointers or pointer manipulation. You pass all parameters, except primitive types, by reference and not by value. As a result, the object identity is preserved. Java does not provide low level, direct access to pointers, thereby eliminating any possibility of memory corruption and leaks.

https://docs.oracle....ne.htm#BABFJIIC

:)
Was This Post Helpful? 0
  • +
  • -

#18 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Java is "always pass by value" - utterly false nonsense

Posted 15 August 2017 - 05:28 PM

But does it pass references by value?
Was This Post Helpful? 0
  • +
  • -

#19 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-December 12

Re: Java is "always pass by value" - utterly false nonsense

Posted 23 August 2017 - 02:01 PM

View Postcfoley, on 16 August 2017 - 03:28 AM, said:

But does it pass references by value?

How else could they be "passed"? You can't really teleport memory locations.

That's the whole idea behind pass-by-reference.

When you pass-by-value you are COPYING(duplicating) an original variable into a method. The method will operate on this COPY which will then be RETURNED somehow.

When you pass-by-reference you are copying the memory location into a method and that address will be used by the method to operate on the ORIGINAL variable(outside the method) without creating any duplicates of that thing.

That's the fundamental difference between pass-by-value vs. pass-by-reference.

Not sure why people have such an obsession with that "swap" thing... it's not even very useful or relevant... it must be C/C++ books that instilled a rather false idea about what pass-by-reference is...

The whole "issue" is basically the difference between:

C++:
int a = 5;
int b = &a;

In this case b points to the address of a, which in turn points to a memory location. If you try to modify the value of a through *b = something; *b is going to use a to get to the memory location a points to. That why reassigning *b a new value will also change a's value, because b is using a in order to change the region of memory a points to.

Java does something equivalent to the following in C++:

int * a = new int(5);
int * b = a;

Basically a new variable(b in this case) will point to the same memory location that a points to, instead of pointing to the address of *a (it can also be done in c++ with pointers to pointers, but java does not work that way). If someone assigns something new to b, that assignment won't affect a in any way. That's the reason why java can't do those "swaps" ... but is that a good enough reason to say that java doesn't use pass-by-reference? I don't think so...

I guess it all boils down to what people understand by "reference" ... is it just the manipulation of memory locations by using variables that can handle such memory locations... should it behave like pointers in C ... or maybe like "references" in C++ (which are really "castrated" pointers)

In my opinion all this debate regarding the various variants of "references" or "pointers" or whatever, is completely irrelevant when it comes to the fundamental characteristics and most common uses of pass-by-value and pass-by-reference, that is, duplicating the original variable and operating on a duplicate vs. operating on the original variable.

This post has been edited by ccdan: 23 August 2017 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#20 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Java is "always pass by value" - utterly false nonsense

Posted 24 August 2017 - 04:01 AM

Quote

If someone assigns something new to b, that assignment won't affect a in any way. That's the reason why java can't do those "swaps" ... but is that a good enough reason to say that java doesn't use pass-by-reference? I don't think so...


I was under the impression that 'b' being an alias for 'a' was the defining characteristic of pass by reference.
Was This Post Helpful? 0
  • +
  • -

#21 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-December 12

Re: Java is "always pass by value" - utterly false nonsense

Posted 24 August 2017 - 08:26 AM

View Postcfoley, on 24 August 2017 - 02:01 PM, said:

I was under the impression that 'b' being an alias for 'a' was the defining characteristic of pass by reference.

Well, the passing mechanism is called pass-by-reference, not pass-by-alias... is a reference the same as an alias?

Besides, I guess it's a bit debatable what an alias is...
Was This Post Helpful? 0
  • +
  • -

#22 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Java is "always pass by value" - utterly false nonsense

Posted 27 August 2017 - 04:19 AM

Fair enough. Let me rephrase my point.

I was under the impression that the symbols 'b' and 'a' referring to the same memory address was the defining characteristic of pass by reference.

I'm not as happy with this phrasing as it hints at an implementation. However, I hope it is specific enough to let us progress with the conversation.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2