[java] final vs const

Started by
13 comments, last by Son of Cain 17 years, 1 month ago
I think we're playing a semantics game here. You could say that it is always "call by value". But you have to qualify that by saying that "objects can only be accessed through references". In other words, when an assignment takes place, the object is not copied but the reference to the object is. To copy an object requires explicity invocation of a clone() function or a copy constuctor. Consider the following code

class Test {

class MyClass {
public int x;
}

void someFunc(int a, MyClass b) {
a = 1;
b.x = 1;
}

public static void main(String argv[]) {

int a = 2;
MyClass b = new MyClass();
b.x = 2;
someFunc(a, b);

System.out.println(a + " " + b.x);
}
}

In a call-by-value passing (like say C), it should print "2 2" and not "2 1" as it does in Java. The object B does not get copied onto the stack, but the function receives a reference to the object. Since the terminology of the Java language is such that pointers to objects are called "references". I think it is not incorrect to say that its pass-by-reference. From someone who comes from a C++ this may seem wrong, but different languages define things in different ways and there is no standard to say that something is "wrong" because a language defines it in a slightly different way.
Advertisement
Quote:Original post by SunDog
You could say that it is always "call by value".

Let's see what the language specification has to say:

Quote:The Java Language Specification, Third Edition, page 211
When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor.

There you have it, black on white.

Quote:Original post by SunDog
But you have to qualify that by saying that "objects can only be accessed through references".

Quote:The Java Language Specification, Third Edition, pages 44/45
There are three kinds of reference types: class types (§8), interface types (§9), and array types (§10). [...]
An object is a class instance or an array.
The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.


Quote:Original post by SunDog
In other words, when an assignment takes place, the object is not copied but the reference to the object is. [...] Consider the following code

Quote:The Java Language Specification, Third Edition, page 46
If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the reference in the other variable.


Quote:Original post by SunDog
void someFunc(int a, MyClass b) {
a = 1;
b.x = 1;
}

In a call-by-value passing (like say C), it should print "2 2" and not "2 1" as it does in Java.

That has nothing to do with call by value. The difference is that "MyClass b" is an object in C, whereas it is a reference to an object in Java.

Quote:Original post by SunDog
I think it is not incorrect to say that its pass-by-reference.

It is incorrect. If it were pass-by-reference, you could assign a different object to b in someFunc, and this would have to have a side effect on the caller (which is not the case, thus it is not pass-by-reference).

With the line b.x = 1; your are not changing b, because the identity of the object is preserved. b still points to the same object. Consider this:

MyClass x = new MyClass();MyClass y = x;x.changeState();assert x == y;MyClass x = new MyClass();assert x != y;

Technically, you are right, the 'value of the reference' is whats passed. I guess my point was, that if you took say a freshman CS student who had just learned C++, then introduced them to Java, if all of what you said was 'Java is call by value', then they would probably expect it to behave like it does in C++. You *can't* pass anything to the function other than the reference to the object 'Value of the object' has no meaning in Java(besides the value of the objects variables)
Quote:Original post by SunDog
\if you took say a freshman CS student who had just learned C++, then introduced them to Java, if all of what you said was 'Java is call by value', then they would probably expect it to behave like it does in C++.

Then just explain to them that Java uses pointers for its object-typed variables. It's better than lying to them that Java is call-by-reference and screwing them up the first time they expect it to actually behave that way.
As far as comparisons to C or C++ are concerned, Java is "call by value" - quotes of the specification above prove that. But people often see it as a "bad thing", fooling themselves that such a behavior is dangerous to performance and other issues. But the average beginner fails to see that this trade-off is exactly what makes Java more interesting, given the arguments already posted by SunDog. Therefore, discussing such aspects is rather pointless for the average programmer, whose choices based on productivity, portability, and other characteristics, are not affected by the concepts discussed above.

But Java is "call by value", yes it is, no matter what the language actually does with pointers and references, you can't argue with that =D
a.k.a javabeats at yahoo.ca

This topic is closed to new replies.

Advertisement