5 ms·
So to summarize, you have a Java function: public void f(Foo bar) { ... } Which is the equivalent C++ function? void f(Foo bar) { ... } void f(Foo& bar
by AnthonyMouse 12y ago
So to summarize, you have a Java function:
public void f(Foo bar) { ... }
Which is the equivalent C++ function?
void f(Foo bar) { ... }
void f(Foo& bar) { ... }
void f(Foo* bar) { ... }
It's the last one. Java passes everything by value, but non-primitive objects in Java are actually pointers (which Java inaccurately calls references), and the pointer is passed by value.
- goalieca 12y agoThis! People say java passes by reference sometimes, but in C++ the references can't be null whereas in java they can.
- theyoungestgun 12y agoCpp and c also call things volatile for entirely different reasons than Java. These are two distinct ecosystems. They use the same terminology with different meaning. That perhaps is confusing, but definitely doesn't make one or the other incorrect.
- Tobani 12y agoI think this is the big thing. Maybe java references arent cpp refs. Is there a proper name for an abstract construct that refers to something else? Maybe reference?
- wspeirs 12y agoWhy is it inaccurate to call it a reference in Java? You don't dereference a variable in Java to access it, so that would make me think reference, not pointer.
- jjnoakes 12y agoBy you do deference object variables in Java. What do you think the "." operator does?
- wspeirs 12y agoAh, true... so what is the difference between a reference and a pointer then?
- jjnoakes 12y agoIt depends on which definition of "reference" you are talking about. Pass-by-reference and C++ references are basically the same - aliasing without requiring dereferencing via an operator. Java object references are like C++ pointers. Java is pass-by-value (no aliasing) for all values (ints, bools, object references etc). Java has no object values.
- cbr 12y ago'a.b()' dereferences 'a' to call its 'b' method. If 'a' is null you'll get a NullPointerException.
- sixbrx 12y agoAlternatively, the NullPointerException could be viewed as a simple leakage of implementation details, which is a common issue with exceptions.
- Tobani 12y agoActually it is somewhere between the last two. I cant do pointer math but i can pass a null reference.
- eslaught 12y agoBut you can't write into the pointer variable itself from inside the callee: static void f(Foo x) { x = ...; } // somewhere Foo y = ...; f(y); The pointer value of y cannot be changed from inside f. Therefore Java is pass-by-value. Java also has restricted semantics on pointers, but that has nothing to do with the calling convention. (Edit: Note that the object which y/x points to can be changed, but that's not the question.)
- Tobani 12y agoSo would you assert this is not pass-by-reference? void f(const Foo& x){ ... } My point is the semantics don't map 1:1. If you're going to say Java's references are pointers with different semantics how is that semantically different from saying Java's references are references with slightly different semantics?
- geocar 12y agovoid f(const Foo&_x){ Foo& x=const_cast<Foo&> (_x); … } What do you think is going on here?
- Tobani 12y ago> What do you think is going on here? I think you're adding code that doesn't address my point. This is pointer manipulation, not function call semantics. If you have raw pointers you can do a lot of magic. I think your code actually demonstrates how not-pointer like Java's references actually are.
- AnthonyMouse 12y agoMaking the referenced object const doesn't change anything. Fill in the '?': void f(Foo* x) { Foo y; *x = y; } void f(Foo& x) { Foo y; x = y; } void f(const Foo* x) { const Foo y; x = &y; } void f(const Foo& x) { const Foo y; ?; }
- mannykannot 12y agoC(++) may not be the best choice for elucidating this issue, given the way arrays decay to pointers. Instead of comparing Java to any other language, would it not be simpler to just say what Java does?