Dimitri Maziuk said:
I have, and I much prefer Delphi language guide that says object
references are "pointers that are automatically dereferenced"
And that's Delphi. It doesn't make sense to say that in Java. In Java,
a reference is only dereferenced if you apply an operation that
dereferences it. Those operations include the "." and "[]" symbols, and
Java 1.5's unboxing conversions. Otherwise, there is no dereferencing
that takes place.
JLS says that there are three "reference types" and that reference types
are *not* primitive types. It says that "reference values" are pointers
to objects.
Well, to be precise, it says that a reference value is either a pointer
to an object, or null.
Then you trace down the three "reference types" and work out
that in the end they're all instantiated as objects, so "reference values"
is what you're interested in.
I'm a little lost by your use of words here. References are just
values. Creating a reference doesn't necessarily imply instantiating an
object. You seem to be talking about something that you call
instantiating a reference type... for which I can't find any logical
meaning. In any case, it's not normal Java terminology.
In 2.10 JLS says that for every parameter, a new variable is created and
initialized when method is invoked. Thankfully, I know enough about
stack-based languages to understand what they mean by that.
I see that language in 4.12.3 in the JLS 3rd edition, and 4.5.3 in the
JLS 2nd edition. Are you sure you're reading the same JLS as the rest
of us?
In any case, I don't know whether you understand this or not. In any
case, this language in the spec doesn't imply a stack. It may or may
not be implemented with the CPU stack (though it obviously is most of
the time for performance reasons). It is similar to, but not quite, an
implementation of the generic computer science concept of stacks --
differing only in that local variables within the current stack frame
may be accessed in any order.
It means just what it says. Every time a method is called, the actual
parameters are copied into the formal parameters, which function as
separate variables. This is pretty close to exactly the definition of
pass by value, as it turns out.
So it's the same as Delphi sans var parameters and clarity of
documentation.
I don't know much about Delphi, but the language above about references
being automatically dereferenced suggests that it's not exactly the
same. If use of a reference as an lvalue in Delphi modifies the object
and not the reference, then it is different in precisely the way that is
needed in order to make Delphi qualify as pass by reference in a way
that Java does not.
Incidentally, if you're look for documentation that makes intuitive
sense to you, then the language spec may not be the place the look.
There are good books on Java, including for example Bruce Eckel's which
is free online. However, if you're looking for precise definition of
language behavior, the spec is the place to look.
What crusade? -- I'm just keepin' up a polite conversation. Prompted
by your statement that "Java references point to the beginnings of
objects, in C++ you can do pointer arithmetics..." It's bad, OK?
I agree that the statement you're quoting is a poor description. It
mixes implementation with specification (in much the same way as the
stack comment above).
2. Unlike Java reference values, C pointers are just memory addresses.
Modern compilers give you syntactic icing on top, like remembering the
type of data poined to and various cast operations, but you're pretty
much free to discard all that sugar and do whatever you want with them
(e.g. work out the address of the second byte in the third field of the
fifth element of an array of records. Sorry, structs.).
I'm sure someone will jump in and point out that the above is only true
if you're willing to achieve undefined behavior according to the C
standard. Nevertheless, your point remains true.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation