Search code examples
javac++terminologysemantics

What do ‘value semantics’ and ‘pointer semantics’ mean?


What is meant by ‘value semantics’, and what is meant by ‘implicit pointer semantics’?


Solution

  • Java is using implicit pointer semantics for Object types and value semantics for primitives.

    Value semantics means that you deal directly with values and that you pass copies around. The point here is that when you have a value, you can trust it won't change behind your back.

    With pointer semantics, you don't have a value, you have an 'address'. Someone else could alter what is there, you can't know.

    Pointer Semantics in C++ :

    void foo(Bar * b) ...
    ... b->bar() ...
    

    You need an * to ask for pointer semantics and -> to call methods on the pointee.

    Implicit Pointer Semantics in Java :

    void foo(Bar b) ...
    ... b.bar() ...
    

    Since you don't have the choice of using value semantics, the * isn't needed nor the distinction between -> and ., hence the implicit.