#clarification on references and pointers

1 messages · Page 1 of 1 (latest)

static bobcat
#

i'm coming from c++ so some things are a little confusing. Apart from the * syntax for pointers not being allowed, is there no such thing as a pointer at all? or rather, are there only pointers in Java? As in, something that stores a memory address to the object we want. Or is it abstracted to a higher level and we don't care about memory addresses at all?

amber vesselBOT
#

<@&987246683568103514> please have a look, thanks.

slender wave
#

when java says:

Person first = new Person();
Person second = first;

then this is the same as:

#
std::shared_ptr<Person> first = std::make_shared<Person>();
std::shared_ptr<Person> second = first;
#

there is no other memory model in java

#

(well, u might argue that primitives are internally stored as values directly. but that isnt really observable by u anyways, so lets ignore that for a moment)

static bobcat
#

so basically what in c are pointers and references are merged into a single concept

slender wave
#

(java calls its memory model "references passed by value" but i find that a bit confusing. so maybe ignore that)

slender wave
#

its std::shared_ptr<Foo> passed-by-value

#

specifically that, and only that

static bobcat
#

gotcha

slender wave
#

its not Person*, its not Person&

#

its just std::shared_ptr<Person> (and those passed by value)

#

👌

static bobcat
#

thanks!

#

i'll keep the thread open if anyone feels like adding something but my question was answered

slender wave
#

(internally the way they are destroyed/garbage collected is ofc different to C++ shared_ptr where its based on ref-counting instead of reachability from roots on the object graph but whatever, details)