Question doesn’t make sense, neither do the answers. There are pointers everywhere in Java; calling them references and using . syntax doesn’t change that.
Isn’t the question about C++?
I’m coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves
“I’m trying to map my totally wrong mental model of Java onto C++.”
I don’t understand what’s wrong with OP’s question. The person didn’t understand stack vs heap allocation in C++, because his experience is in Java, so he asked about it. He doesn’t claim there aren’t pointers everywhere in Java. Seems fair to me.
I think a simpler answer would have been “In Java, all those ‘objects’ you thought you were using were really pointers.” Boom, the light goes on.
The accepted answer isn’t wrong per se, but given the OP’s lack of understanding, I don’t see how they could have possibly comprehended half the answer they accepted. Sounds legit, check mark.
I would love to see OP’s code before and after this advice. My bet is they are in for a world of hurt, either returning stack references or dealing with buggy copy constructors, let alone the insane compiler optimizations that can cause the compiler to elide or not elide any number of constructors and assignment operations.
Mostly I’m confused why this is on lobsters.
It frequently seems to me that the terminology Java adopted does a disservice to programmers trained with it. It cannot possibly be the case that pointers are arcane and incomprehensible while references are a simple enough concept that all of the millions of Java programmers can understand and use them when, as you point out, they’re equivalent. (Though maybe I’m giving many Java programmers too much credit.) It seems to me that calling references “managed pointers” or the like would save a lot of intimidation and the need for an epiphany moment realizing pointers are not a scary new concept.
Pardon the meta-talk.. but it is sad to see that someone has downvoted tedu’s comment without explaining why. I thought that was a HN phenomenon we try to avoid here.
What is it exactly that doesn’t make sense? The wording may be imperfect (what are “the object themselves”?), but the examples make it clear what the person is asking. The highest ranking answer may not be encyclopedically complete, but does a great job at pointing the reader in the right directions.