tags:

views:

950

answers:

4

what does it mean by value semantics and what is mean by implicit pointer semantics?

+5  A: 

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.

David Pierre
A: 

Java uses implicit pointer semantics on variable access (you can not directly edit the reference, it autmatically (implicit) gets resolved to the Object on access) and also uses Pass-by-Value semantics on method parameters passing.

Read Pass-by-value semantics in Java applications:

In Java applications, when an object reference is a parameter to a method, you are passing a copy of the reference (pass by value), not the reference itself. Note that the calling method's object reference and the copy are pointing to the same object. This is an important distinction. A Java application does nothing differently when passing parameters of varying types like C++ does. Java applications pass all parameters by value, thus making copies of all parameters regardless of type.

Short: All parameters in Java are passed by value. But that doesn't mean an Object gets copied (like the default in PHP4), but the reference to that object gets copied.

You'll see all explanations and in-depth examples on Pass-by-value semantics in Java applications

Andre Bossard
+2  A: 

Basically, value semantics means that assigning one value to another creates a copy:

int x = 1;
int y = x;
x = 2; // y remains the same!

A special case is a function call which gets passed an argument:

void f(int x) {
    x = 5;
}

int a = 1;
f(a);
// a is still 1

This is actually the same for Java and C++. However, Java knows only a few primitive types, among them int, double, boolean and char, along with enums which behave in this manner. All other types use reference semantics which means that an assignment of one value to another actually redirects a pointer instead of copying the underlying value:

class Foo {
    int x;

    public Foo(int x) { this.x = x; }
}

Foo a = new Foo(42);
Foo b = a; // b and a share the same instance!
a.x = 32;
//b.x is now also changed.

There are a few caveats however. For example, many reference types (String, Integer …) are actually immutables. Their value cannot be changed and any assignment to them overrides the old value.

Also, arguments still get passed by value. This means that the value of an object passed to a function can be changed but its reference can't:

void f(Foo foo) {
    foo.x = 42;
}

void g(Foo foo) {
    foo = new Foo(42);
}

Foo a = new Foo(23);
f(a);
// a.x is now 42!

Foo b = new Foo(1);
g(b);
// b remains unchanged!
Konrad Rudolph
+2  A: 

Java is pass by value, C++ can use both, value and reference semantics.

http://javadude.com/articles/passbyvalue.htm

Bartosz Blimke
That description is misleading because of Java's hidden pointers (even if the pointers themselves are passed by value.)
finnw
@finnw : I agree, but then, Java developers should still understand it that way, because if they hope to change the pointed data inside a function, they will be surprised the original "reference" outside is left untouched. So this is important for them to understand their "references" are copied.
paercebal
Bartosz: glad to see people spreading my article ;)finnw: "Hidden" is the key word there...We're talking about how someone programs in Java.Implementation mechanics behind the scenes don't change the fact that Java is strictly pass-by-value -- read the Java Language Spec to see it spelled out.
Scott Stanchfield