> Some simple rules:
>
> * C++ pointers are called "references" in Java.
Is this really true? I thought that Java references were 64 bits (8
bytes) on 32 bit systems, meaning that Java references are twice as big
as a pointer and there's therefore more going on in there than a simple
pointer.
Does anyone know what common JVM implementations stuff in a reference
besides just a pointer?
> There is nothing in Java
> like a C++ reference.
Well, I think references in Java are a lot like references in C++. :-)
Java doesn't have the same operations on a reference that C++ does, but
the actual reference itself is similar and fulfills a similar purpose.
> * There is no such thing as explicit dealocation in Java (i.e.no delete
> operator).
I think you can force garbage collection in Java. Or fine tune it.
> * Objects are made up of scalars and pointers. Unlike in C++, one object
> never contains another.
Well, conceptually the reference is the object. References in Java can
be used for aggregation. But literally yes, Java doesn't make objects
contiguous in memory within another object.
Just my 2 bucks. I don't mind being wrong, sometimes I even learn
something.
Oliver Wong - 18 Jul 2006 20:34 GMT
>> * There is no such thing as explicit dealocation in Java (i.e.no delete
>> operator).
>
> I think you can force garbage collection in Java. Or fine tune it.
You can ask for garbage collection by calling System.gc(), but it's a
legal implementation for the JVM to ignore the request.
Some JVMs accept command line arguments so that you can select between
different flavours of garbage collectors.
- Oliver
Mike Schilling - 18 Jul 2006 20:50 GMT
>> Some simple rules:
>>
[quoted text clipped - 4 lines]
> a pointer and there's therefore more going on in there than a simple
> pointer.
There's no guarantee of the size of a pointer in either Java of C++.
Anyway, I'm talking about how they're used, not how they're implemented. If
you're a C++ programmer learning Java, you're better off thinking of them as
pointers.
>> There is nothing in Java like a C++ reference.
>
> Well, I think references in Java are a lot like references in C++. :-)
> Java doesn't have the same operations on a reference that C++ does, but
> the actual reference itself is similar and fulfills a similar purpose.
The main thing that distnguishes a C++ reference from a C++ pointer is that
the reference can't be reset: it points to the same object for its entire
lifetime. This is not true of Java references. Likewise, Java references
can be null, while C++ references cannot.
Note that the exception that occurs when trying to dereference a Java
reference is a NullPointerException; they pretty clearly got renamed to
"reference" at some point.
>> * There is no such thing as explicit dealocation in Java (i.e.no delete
>> operator).
>
> I think you can force garbage collection in Java. Or fine tune it.
You can give the GC hints, with no guarantee they'll have any effect.
Unlike in C++, you cannot say "Tear down this specific object, now, and free
its memory." That's why, where it's very common to put cleanup logic in C++
destructors, it's uncommon to put similar logic in Java finalizers: because
there's no guarantee when (or whether) the finalizers will run.
>> * Objects are made up of scalars and pointers. Unlike in C++, one object
>> never contains another.
>
> Well, conceptually the reference is the object. References in Java can be
> used for aggregation. But literally yes, Java doesn't make objects
> contiguous in memory within another object.
Well, it might In Java, you don't really know anything about how things
are laid out in memory :-)
In C++, when object B is contained inside A, their lifetimes are connected.
B is constructed as part of A's construction, and the same with destruction.
B cannnot be deleted except as a side-effect of deleting A. In Java, A can
point to B, but if something else points to B as well, B can outlive A.
That is C++ can both aggregate objects directly and make trees of objects
with pointers (or references). Java only has trees.
Chris Uppal - 19 Jul 2006 08:40 GMT
> Is this really true? I thought that Java references were 64 bits (8
> bytes) on 32 bit systems, meaning that Java references are twice as big
> as a pointer and there's therefore more going on in there than a simple
> pointer.
Typical JVM implementations use raw pointers to implement Java's references --
i.e. 32-bits on a 32-bit machine
> Does anyone know what common JVM implementations stuff in a reference
> besides just a pointer?
Nowt.
FWIW, I haven't heard of /any/ JVM implementation which uses tagged pointers --
though I imagine that implementations which run on tagged hardware will do. In
fact I haven't heard of software-only tagging used for any language
implementation since Icon. Doesn't mean there aren't implementations I don't
know about, though...
-- chris