Now I see why Java only has objects, no references.
The above is wrong.
<
http://www.yoda.arachsys.com/java/passing.html>
"The values of variables are always primitives or references, never
objects."
The designers of C++ sacrificed readability for this albeit clever
feature of references.
Not that I'm trolling mind you, the two approaches aren't better or
worse per se, just different.
Just to be clear. The difference between the two languages when it
comes to this particular issue isn't about "references", rather it's
about how an object is initialized. In Java, all fields are default
initialized, in C++ the programmer has control over the field's
initialization. For example:
class Foo {
int bar;
public:
Foo(): bar(5) { }
};
In the above C++ code 'bar' is *created* with the value of 5.
class Foo {
private int bar;
public Foo() {
bar = 5;
}
}
In the above Java code, 'bar' is created with the value of 0 and then
assigned the value of 5. Note, near the same thing would happen if we
used equivalent code in C++ except 'bar' would be created with some
undefined value, then assigned the value of 5.
When it comes to ints, the separate creation/assignment steps don't
much matter, but if we were using a type that is expensive to create
and expensive to assign, then being able to save a step is helpful.