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

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3516 Views - Last Post: 24 October 2017 - 12:35 AM

#1 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 18 June 2017 - 03:50 AM

Yes, I'm referring to objects.

If James Gosling said that, it must be true, right?

Whenever someone dares to say that java objects are passed by reference , an army of people(some are guru's blind followers, some are people who love repeating what they read on the web) jump at heretic's throat with the following reply(supposedly the "divine truth"): "Java is always pass by value, the address of the reference is being passed(copied)" usually followed by an example of a method trying to swap two objects and the failure of such an attempt being considered the "proof" that java is not "pass by reference." Sometimes, some people come up with other terms, like "pass by object" or something like that.

OK, let's explore two issues with the above "divine truth" that the web is full of:

1 - Java is always pass by value, the address of the reference is being passed(copied) - Why the heck do we keep hearing such a statement? Is there any other way to achieve pass by reference other than by copying the address of an object into a method parameter? Is it possible to somehow "move" physical(or virtual) memory addresses into method parameters? Or is it possible to somehow move methods around physical memory addresses? I'm not aware of such a thing(and it doesn't even make sense to me.) If you know better, feel free to illuminate me, but until then I'll say this: if memory addresses can't be moved around, then they must copied somehow, more or less transparently. I don't think any language can achieve pass by reference other than by copying the addresses being referenced.

a method trying to swap two objects and the failure of such an attempt being considered the "proof" that java is not "pass by reference." - why do we keep seeing this example? Is that the main purpose of pass by reference? To swap objects? To point the original reference to a different object? Really? I don't think so. As far as I know, the main purpose of pass by reference is to avoid the duplication of data(objects in java's case) when passed to methods. That way, methods can operate on the original data, thus saving memory and computing time. This explanation is what you'll find in most books or other credible references when explaining the advantages of pass by reference over pass by value, and I could bet real money, that's how most real world code uses pass by reference in language that support the feature. And that's what java supports when it comes to objects. Why do so many people keep coming with this irrelevant nonsense of swapping objects?

The fact that java can't make the original reference point to another object from within a method is just a language feature, as are other language features like not having multiple inheritance, or having to use exception handling in various situations. Can we say that java does't allow inheritance because it doesn't allow multiple inheritance? I don't think so.

Maybe I'm missing something. If you know better, feel free to "illuminate" me(and others like me) but please provide arguments.

This post has been edited by ccdan: 18 June 2017 - 03:51 AM


Is This A Good Question/Topic? 0
  • +

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

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

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

Posted 18 June 2017 - 04:29 AM

At the lowest level, everything is passed by value, as even memory addresses are in fact values. For practical purposes though, that's not really a useful way to look at the thing.

I don't know what Java does, precisely, but assuming it doesn't naively duplicate every object every time it is passed anywhere, at least from a user-level perspective I would consider it pass-by-reference even if that is not exactly how it is implemented deep down in the guts of the language.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

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

Posted 18 June 2017 - 04:41 AM

In my mind pass-by-object means passing around pointers to objects and pass-by-reference means passing around pointers to variables. To me, that's a perfectly reasonable distinction to make.

View Postccdan, on 18 June 2017 - 12:50 PM, said:

1 - Java is always pass by value, the address of the reference is being passed(copied) - Why the heck do we keep hearing such a statement? Is there any other way to achieve pass by reference other than by copying the address of an object into a method parameter?


Yes, by passing the address of the variable.

Quote

a method trying to swap two objects and the failure of such an attempt being considered the "proof" that java is not "pass by reference." - why do we keep seeing this example?


Because that's the most simple example where pass-by-reference semantics would be useful, I suppose. The most simple method that's easy to define in languages with pass-by-reference, but impossible to define in Java.

Quote

Is that the main purpose of pass by reference? To swap objects? To point the original reference to a different object? Really?


I don't know about "main purpose", but the defining characteristic of pass-by-reference is to be able to assign a passed-in variable to something else, yes.

Quote

As far as I know, the main purpose of pass by reference is to avoid the duplication of data(objects in java's case) when passed to methods. That way, methods can operate on the original data, thus saving memory and computing time. This explanation is what you'll find in most books or other credible references when explaining the advantages of pass by reference over pass by value, and I could bet real money, that's how most real world code uses pass by reference in language that support the feature.


Certainly not in C# (which supports both pass-by-object and pass-by-reference). There pass-by-reference is used to simulate multiple return values or to implement swap-style methods. For what you describe, the standard pass-by-object semantics suffice.

In C and C++ it's also a common pattern to use pointers-to-variables (C) or references (C++) to simulate multiple return values and to implement swap-style functions (note that std::swap even is a standard library function in C++), but pointers and references are of course also used for the reasons you describe.

Quote

Can we say that java does't allow inheritance because it doesn't allow multiple inheritance? I don't think so.


No, but we can say that Java doesn't allow multiple inheritance.

This post has been edited by sepp2k: 18 June 2017 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#4 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 18 June 2017 - 07:20 AM

View PostAtli, on 18 June 2017 - 02:29 PM, said:

I don't know what Java does, precisely, but assuming it doesn't naively duplicate every object every time it is passed anywhere, at least from a user-level perspective I would consider it pass-by-reference even if that is not exactly how it is implemented deep down in the guts of the language.

No, it doesn't duplicate objects.

View Postsepp2k, on 18 June 2017 - 02:41 PM, said:

In my mind pass-by-object means passing around pointers to objects and pass-by-reference means passing around pointers to variables. To me, that's a perfectly reasonable distinction to make.

What's the usefulness of such a distinction? In both cases we have pointers that are being passed around, in both cases we don't have duplicates of the contents those pointers point to. It's pretty much the same thing to me.

However, my main gripe is with statements that pretend that java always uses "pass by value" ... like the following:

From the authors of Java: "There is exactly one parameter passing mode in Java - pass by value - and that helps keep things simple." The Java Programming Language, 2nd ed. by Ken Arnold and James Gosling, section 2.6.1, page 40, 3rd paragraph.


To me, when it comes to pass-by-whatever, that whatever gets duplicated, that is, copied into a parameter variable/constant of the same type.

If we have pass-by-reference, then, a memory address is being duplicated(copied from the original reference variable, called pointer in C/C++ and reference variable in Java - both hold memory addresses, into another variable that holds addresses). Methods that get references passed to, will operate on the data in the original location(not on duplicates)

if we have pass-by-value, then, the value that's present at a certain memory location, gets completely duplicated(copied from the original source, to another place in memory - whether we're talking about a primitive variable or an object). Methods that get values passed to, will operate on the data that has been duplicated.

Is there anything wrong with that view?

View Postsepp2k, on 18 June 2017 - 02:41 PM, said:

Yes, by passing the address of the variable.

What's the distinction between "passing" and "copying" a memory address to/into a memory address holding variable(pointer, reference variable or whatever)?

View Postsepp2k, on 18 June 2017 - 02:41 PM, said:

Certainly not in C# (which supports both pass-by-object and pass-by-reference). There pass-by-reference is used to simulate multiple return values or to implement swap-style methods. For what you describe, the standard pass-by-object semantics suffice.

In C and C++ it's also a common pattern to use pointers-to-variables © or references (C++) to simulate multiple return values and to implement swap-style functions (note that std::swap even is a standard library function in C++), but pointers and references are of course also used for the reasons you describe.

While I'm not very experienced with C/C++, I don't really see the need for such a thing when you have structs(in C). I'm not familiar with C# either, but knowing it's similar to Java, such "exercises" of simulating multiple return values are probably completely pointless. And I'm still not sure where that swapping ability comes into play.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

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

Posted 18 June 2017 - 11:25 AM

View Postccdan, on 18 June 2017 - 04:20 PM, said:

However, my main gripe is with statements that pretend that java always uses "pass by value"


I agree that that's confusing, which is why I prefer the term call-by-object.

Quote

If we have pass-by-reference, then, a memory address is being duplicated(copied from the original reference variable, called pointer in C/C++ and reference variable in Java - both hold memory addresses, into another variable that holds addresses). Methods that get references passed to, will operate on the data in the original location(not on duplicates)


This isn't quite accurate for C and C++. Sure, if you do T* x = new T(...); f(x); you're passing a copy of the address stored in x. That's basically pass-by-object. However when we talk about pass-by-reference, we usually mean something like T x; f(&x); in C or T x; f(x); in C++ (where f was declared to take a reference). In that case we're not passing the address that was stored in x (x does not contain an address), we're passing the address of x. Since the address of x was not previously stored anywhere, it isn't really accurate to say that we copied it.

Quote

What's the distinction between "passing" and "copying" a memory address to/into a memory address holding variable(pointer, reference variable or whatever)?


I wasn't trying to make a distinction between passing and copying, but between the address of the object and the address of the variable. Though as I said above, when we pass the address of the variable, we're not really copying it from anywhere.

View Postsepp2k, on 18 June 2017 - 02:41 PM, said:

While I'm not very experienced with C/C++, I don't really see the need for such a thing when you have structs(in C).


I suppose there's a bit of a performance difference (if you return a struct, you usually can't return it via a register) and using a struct is a bit more cumbersome as you need to define a struct. Either way the multiple-return via pointer/reference pattern is much more common in C and C++ than multiple-return via struct.

Quote

I'm not familiar with C# either, but knowing it's similar to Java, such "exercises" of simulating multiple return values are probably completely pointless.


It's the same argument as for C and C++ I suppose. And I wouldn't really call it an exercise since it's used in the standard library. That is, I don't think the designers designed the standard library this way as an exercise.

Quote

And I'm still not sure where that swapping ability comes into play.


It's not incredibly useful, but it does save you a temporary variable. And in C++ it saves you from copying large objects by making use of move semantics (of course you could do the same thing by hand, but then you'd have to figure out how move semantics work on your own).




In summary I wouldn't say that the distinction between call-by-reference and call-by-object isn't necessarily major, but if you call it call-by-reference this will inevitably confuse people coming from languages with real call-by-reference semantics (and people who are simply familiar with those semantics).

I've encountered a non-trivial amount of Java programmers who assumed re-assigning a parameter will re-assign the variable that was passed as an argument because they were told Java is pass-by-reference. So why risk that confusion?
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10731
  • View blog
  • Posts: 18,359
  • Joined: 19-March 11

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

Posted 18 June 2017 - 12:53 PM

View Postccdan, on 18 June 2017 - 05:50 AM, said:

Maybe I'm missing something. If you know better, feel free to "illuminate" me(and others like me) but please provide arguments.


Steering around the minefield of catchphrases, I'll just say that the important thing is to remember that java always passes a value, and that the value is sometimes a reference to an object. Understanding this statement is one of the early milestones of understanding java, and indeed most or all object-oriented programming languages. You seem to have passed this milestone, but you're getting hung up on the language around it. I wouldn't worry about it so much: you've got the point, move on to the next thing.
Was This Post Helpful? 2
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10731
  • View blog
  • Posts: 18,359
  • Joined: 19-March 11

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

Posted 18 June 2017 - 05:00 PM

View PostAtli, on 18 June 2017 - 06:29 AM, said:

I don't know what Java does, precisely, but assuming it doesn't naively duplicate every object every time it is passed anywhere, at least from a user-level perspective I would consider it pass-by-reference even if that is not exactly how it is implemented deep down in the guts of the language.


Java in fact passes a pointer to the object on the heap, which the compiler uses (presumably making use of the type declaration as well) to determine what can legally be done with the object. Nothing is duplicated.

A true "call by reference" in my mind would still be implemented by passing a pointer, but it would essentially treat that pointer as a pointer to a global, such that if we have an int parameter a, and we execute a+=1; then whatever variable was passed by the calling function would experience that increment. This would of course mean that you would have to come up with a way to handle literal values as arguments to functions - we wouldn't want to increment 5 and have it stick! - but that could probably be resolved without too much bother for the user.

It would also make for a truly awful language, in my opinion, but I expect there are people who would look on it as a Good Thing to be able to have a function to swapValues(obj1, obj2). These people, of course, are why language design is usually done by people with more experience.
Was This Post Helpful? 0
  • +
  • -

#8 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 18 June 2017 - 10:02 PM

View Postsepp2k, on 18 June 2017 - 09:25 PM, said:

However when we talk about pass-by-reference, we usually mean something like T x; f(&x); in C or T x; f(x); in C++ (where f was declared to take a reference).

I'm not sure what you're trying to say. As far as I know references (&) in c++ are implemented using pointers. Besides, C++ references behave much like java reference variables in methods, that is, they can't be reassigned.

View Postsepp2k, on 18 June 2017 - 09:25 PM, said:

In that case we're not passing the address that was stored in x (x does not contain an address), we're passing the address of x. Since the address of x was not previously stored anywhere, it isn't really accurate to say that we copied it.

It's basically the same. Whether we pass a variable that hold an address(a pointer) or a variable whose address is figured out by some mechanism(as it happens with references), the idea is that an address is being copied, either from a variable/constant or from a memory location where a certain primitive variable/object resides, that's figured out somehow by a certain mechanism of the language.

View Postsepp2k, on 18 June 2017 - 09:25 PM, said:

Since the address of x was not previously stored anywhere, it isn't really accurate to say that we copied it.

Addresses are always stored somewhere if the data/objects are stored in memory, otherwise they couldn't be used by programs. Each and every variable and both c++ and java, have(at least): their own address and an address of a memory location they point to. User access to those addresses and the ability to see or manipulate them depends on language, variable type, context and so on. But it doesn't mean that they don't exist.

This post has been edited by ccdan: 18 June 2017 - 10:03 PM

Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,707
  • Joined: 18-April 07

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

Posted 18 June 2017 - 10:58 PM

Wow, talk about really wasting a lot of time talking about all this. Java is pass by value... it is even stated on the official Java docs page and explains why it is so...

https://docs.oracle..../arguments.html


As for pass by ref vs value, there is a simple distinction. When you pass by reference you are not copying the address the "pointer" is pointing to, you are referring to the actual pointer (like an alias). When you pass by value, a copy of that address is copied to a new location. This explains why when you call on methods of the passed in object you can alter the object, but then when you assign a new object it will not change the original pointer after the method returns.

This is explained on that page I reference under the title "Passing Reference Data Type Arguments". You really shouldn't read into it any more than that and try to reconcile it with languages that actually pass by reference with pointers and such. Otherwise you are just going to drive yourself, and possibly others, mad. ;)
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

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

Posted 18 June 2017 - 11:25 PM

But if we can't compare our language to other languages - to passive-aggressively shove our own language's superiority in the others faces - then what would we do with our time?
Was This Post Helpful? 1
  • +
  • -

#11 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 19 June 2017 - 02:04 AM

View PostMartyr2, on 19 June 2017 - 08:58 AM, said:

When you pass by value, a copy of that address is copied to a new location.

That's when you pass-by-reference!

When you pass by value, you are copying the whole content that exists at the location a certain variable(passed as a parameter) points to. You are creating a duplicate of that thing, regardless of whether it's a simple primitive variable or an object.

Despite my modest c++ skills, I hope the following example makes clear the distinction between pass by value and pass by reference:

#include <iostream>

using namespace std;

int x = 5;
int *p = &x;
void passbyvalue(int y) {
    int *q = &y;
    cout << "passed value inside method is " << x << " and it's going to be changed to 10" << endl;
    cout << "the address of x(global) equals the address of parameter(y) " << (p == q) << endl;
    y = 10;
}

void passbyreference(int& y) {
    int *q = &y;
    cout << "passed value inside method is " << x << " and it's going to be changed to 10" << endl;
    cout << "the address of x(global) equals the address of parameter(y) " << (p == q) << endl;
    y = 10;
}

int main()
{
    cout << "global x: "  << x << endl;
    passbyvalue(x);
    cout << "global x after passbyvalue: " << x << endl;
    passbyreference(x);
    cout << "global x after passbyreference:" << x << endl;

    return 0;
}



I can't believe this can be so confusing to some of you!

View PostMartyr2, on 19 June 2017 - 08:58 AM, said:

Wow, talk about really wasting a lot of time talking about all this. Java is pass by value... it is even stated on the official Java docs page and explains why it is so...

Apparently you didn't read my firs post:

View Postccdan, on 18 June 2017 - 01:50 PM, said:

Yes, I'm referring to objects.

If James Gosling said that, it must be true, right?


That's precisely what's I'm trying to counter: complete nonsense that's regarded as an absolute "truth" because it's been preached by java's "guru" (or oracle docs if you want)
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

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

Posted 19 June 2017 - 03:53 AM

View Postccdan, on 19 June 2017 - 07:02 AM, said:

I'm not sure what you're trying to say. As far as I know references (&) in c++ are implemented using pointers.


Yes.

Quote

Besides, C++ references behave much like java reference variables in methods, that is, they can't be reassigned.


No. When people say that C++ references can't be reassigned, they mean that a C++ reference can't be made to refer to something else. You can easily do this in Java via x = somethingElse. If you did that with a C++ reference, you'd change the value of the referred-to object instead of making x refer to something else (that's what you can't do in Java). So what you can and can't do is pretty much reversed vs. Java.

I think the best way to think about it is with C pointers. If x is a pointer you can either do x = someNewPointer;, making x point somewhere else, or *x = someNewValue;. In Java all assignments do the former and with C++ references all assignments do the latter.

Quote

Addresses are always stored somewhere if the data/objects are stored in memory


Well the stack pointer is stored somewhere and the offset of a given variable is stored in the machine code as a constant. The actual value of SP+offset isn't stored anywhere unless you take the address of a variable. So I wouldn't necessarily call that copying. But as I said, copying vs. passing isn't really the distinction I was trying to make, so that's a whole big tangent.

All I wanted to say is that if you have T* x; and you do f(x) (in C where there's only pointers, no C++ references), f won't be able to make x point somewhere else, but if you do f(&x) it will be. That's the distinction between call-by-value and call-by-reference in my mind. No C programmer I know calls f(x) call-by-reference. f(&x) is call-by-reference and f(x) is call-by-value. Java does f(x), which is why Gosling calls it call-by-value. This is confusing because in Java you don't see the fact that x is a pointer, but call-by-reference is equally confusing because you're passing x, not &x. That's why people invented a new word.

View Postjon.kiparsky, on 19 June 2017 - 02:00 AM, said:

It would also make for a truly awful language, in my opinion, but I expect there are people who would look on it as a Good Thing to be able to have a function to swapValues(obj1, obj2). These people, of course, are why language design is usually done by people with more experience.


Is that meant as a dig versus C# and C++ or are you making a distinction between what you describe and how references in C++ and C# work? The "would" kinda implies the latter, but isn't what you described exactly how those languages work? Or are you saying it'd be awful if that was the default behavior?
Was This Post Helpful? 0
  • +
  • -

#13 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 22 June 2017 - 12:36 AM

View Postsepp2k, on 19 June 2017 - 01:53 PM, said:

No. When people say that C++ references can't be reassigned, they mean that a C++ reference can't be made to refer to something else.

That's exactly what I meant.

View Postsepp2k, on 19 June 2017 - 01:53 PM, said:

You can easily do this in Java via x = somethingElse. If you did that with a C++ reference, you'd change the value of the referred-to object instead of making x refer to something else (that's what you can't do in Java). So what you can and can't do is pretty much reversed vs. Java.

No. If x was declared outside a method and you try to make x point to somewhere else from within a method that received x as an argument, it would have no effect outside the method, x will point to the same original content)

View Postsepp2k, on 19 June 2017 - 01:53 PM, said:

Well the stack pointer is stored somewhere and the offset of a given variable is stored in the machine code as a constant. The actual value of SP+offset isn't stored anywhere unless you take the address of a variable. So I wouldn't necessarily call that copying. But as I said, copying vs. passing isn't really the distinction I was trying to make, so that's a whole big tangent.

Well, if an object has an address and if that address finds itself in a variable, without having been "moved" into that variable(if that makes any sense), then it must have been copied somehow. Not sure about the technicalities you're describing, since java really needs those addresses all the time, but it's about the same as copying to me.

View Postsepp2k, on 19 June 2017 - 01:53 PM, said:

f won't be able to make x point somewhere else, but if you do f(&x) it will be. That's the distinction between call-by-value and call-by-reference in my mind. No C programmer I know calls f(x) call-by-reference. f(&x) is call-by-reference and f(x) is call-by-value. Java does f(x), which is why Gosling calls it call-by-value.

I don't understand why such an irrelevant aspect would be considered a "defining aspect" of pass by reference, when almost all the explanations and actual real-world uses point to the distinction between the ability of a method to operate on the original data(via references) vs on a copy of the data(pass by value) - that has always been the fundamental feature of pass-by-ref vs pass-by-val, but when it comes to this java discussion everyone is all of a sudden interested in making the original variable point to something else.

By the way, std::swap is rarely used. I've been digging through some open source code and most are false positives(custom methods also called swap, not the std version) - just 6 instances for std::swap in over 4000 source code files.
Was This Post Helpful? 0
  • +
  • -

#14 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

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

Posted 22 June 2017 - 01:46 AM

View Postccdan, on 22 June 2017 - 09:36 AM, said:

No. If x was declared outside a method and you try to make x point to somewhere else from within a method that received x as an argument, it would have no effect outside the method, x will point to the same original content)



I think we're talking past each other: Here's how Java works:

void f() {
  T obj = something;
  g(obj);
}

void f(T x) {
  // Until now x points to something
  x = somethingElse;
  // x now points to somethingElse, obj hasn't changed
}



In C++:

void f() {
  T obj = something;
  g(obj);
}

void f(T& x) {
  // Until now x refers to obj
  x = somethingElse;
  // x still refers to obj, but the value of obj has now changed to somethingElse
}



So it is in fact the opposite. In Java I can break the connection between x and obj simply by re-assigning x. In C++ I can't break the connection, but I can easily change the value of obj through x.

Quote

Well, if an object has an address and if that address finds itself in a variable, without having been "moved" into that variable(if that makes any sense), then it must have been copied somehow.


But we're not talking about the address of an object when passing by reference - we're talking about the address of a variable.
Was This Post Helpful? 0
  • +
  • -

#15 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

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

Posted 22 June 2017 - 02:34 AM

View Postsepp2k, on 22 June 2017 - 11:46 AM, said:

I think we're talking past each other: Here's how Java works:

void f(T x) {
  // Until now x points to something
  x = somethingElse;
  // x now points to somethingElse, obj hasn't changed
}



In C++:

void f() {
  T obj = something;
  g(obj);
}

void f(T& x) {
  // Until now x refers to obj
  x = somethingElse;
  // x still refers to obj, but the value of obj has now changed to somethingElse
}



So it is in fact the opposite. In Java I can break the connection between x and obj simply by re-assigning x. In C++ I can't break the connection, but I can easily change the value of obj through x.

You're referring to something entirely irrelevant: changing what a formal parameter points to. That's not what I was trying to say about java(a pretty useless feature if you ask me, unless it has an effect on the actual parameter)

This what I'm talking about:

public class Test {

	public static void main(String[] args) {

		StringBuilder externalVar = new StringBuilder("abc");
		System.out.println("external var:" + externalVar);
		modify(externalVar);
		System.out.println("external again: " + externalVar); // this shows that reassignament of original external variable not possible from within a method
		

	}
	
	public static void modify(StringBuilder s) {
		s.append("xxx"); // <- this shows that pass by ref actually works
		s = new StringBuilder("def");
		System.out.println("Inside method modify: " + s);
	}
}


How does this differ from c++ references?

Quote

But we're not talking about the address of an object when passing by reference - we're talking about the address of a variable.

An address of whatever. But it's an address and that's the whole point. When we have pass by value we have a duplicate of the actual data an address points to.

This post has been edited by ccdan: 22 June 2017 - 02:42 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2