Remix.run Logo
_old_dude_ 8 hours ago

I think pass by copy is a consequence of being modifiable.

The other solution is to stack allocate and pass a pointer but as i said, unlike in C#, i do not think it's possible to do that in Java.

In Go, you can stack allocate but when you send a pointer (that escapes), the compiler will heap allocate the object.

layer8 8 hours ago | parent [-]

My point is that pass by copy and pass by value do the same thing, they copy the value representation. In other words, pass by copy means exactly pass by value.

gf000 7 hours ago | parent | next [-]

Actually, Java only has pass-by-value, even for reference types. (The same way as C does).

People really misuse/misunderstand this term: Java objects are passed by their pointers ("references") being copied.

The alternative is pass by reference, which is done by e.g. c++, rust, who actually have references (Java doesn't). A good litmus test is whether you can write a swap method that actually changes your local variables.

steveklabnik 2 hours ago | parent [-]

Rust is also “pass reference by value,” not pass by reference.

_old_dude_ 7 hours ago | parent | prev [-]

For me, the difference is that if methods are inlined, the compiler is still required to do a copy for structs but not for value classes.

I do not know how this is called.