Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...