A
axter
Whenever I read a Java book and it states that Java has no pointers, it
makes my teeth grind. It would be more accurate to say Java only uses
pointers.
That's right. Java only uses pointers.
However, that also wouldn't be entirely accurate either, but it would
be closer to the truth then to say Java doesn't use pointers.
When these books make these claims they're usually explicitly or
implicitly comparing the Java memory addressing method to the C/C++
memory addressing methods.
C++ has three main methods for addressing memory. (concrete, pointer,
and reference)
Concrete address
int x = 10;// x is a concrete variable
Pointer address
int *px = &x; //px is a pointer variable
Reference address
int &rx=x; //rx is a reference variable
Most Java books would have you believe that Java's memory method is
not a pointer method, and that it's memory methods are more like the
C/C++ concrete method.
However, if the characteristics of all three methods are examine,
you'll find that Java's memory method characteristics has more in
common with the C++ pointers, and have very little in common with the
concrete method.
In C++, when a concrete variable is declared, its constructor is always
called
When a reference variable is declared, a constructor is never called.
When a pointer variable is declared a constructor is called only if it
is initialized by pointing to a NEW-operator.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, a concrete variable must be initialized to address valid
memory.
A reference variable must also be initialized to address valid memory.
A pointer may be initialized to point to nothing at all, or to point to
valid memory.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, a concrete variable (once initialized) can never change the
memory it's addressing.
A reference variable (once initialized) can never change the memory
it's addressing.
A pointer can change what it's pointing to at any time.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, you cannot assign a concrete variable to new operator.
A reference variable cannot be directly assigned to a new operator.
A pointer can be directly assigned to a new operator.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, pointer arithmetic cannot be performed on a concrete type.
Pointer arithmetic cannot be performed on a reference type.
Pointer arithmetic can of course be performed on a pointer variable.
This is where Java memory modal and C++ pointers differ. You cannot
perform pointer arithmetic on a Java address method.
Other then pointer arithmetic, Java's memory modal is very much a C++
pointer memory modal.
So if a Java book really wanted to be more accurate, it would say:
"Java only uses pointers, and Java pointers lack the ability to
perform pointer arithmetic."
OK, frag away!
makes my teeth grind. It would be more accurate to say Java only uses
pointers.
That's right. Java only uses pointers.
However, that also wouldn't be entirely accurate either, but it would
be closer to the truth then to say Java doesn't use pointers.
When these books make these claims they're usually explicitly or
implicitly comparing the Java memory addressing method to the C/C++
memory addressing methods.
C++ has three main methods for addressing memory. (concrete, pointer,
and reference)
Concrete address
int x = 10;// x is a concrete variable
Pointer address
int *px = &x; //px is a pointer variable
Reference address
int &rx=x; //rx is a reference variable
Most Java books would have you believe that Java's memory method is
not a pointer method, and that it's memory methods are more like the
C/C++ concrete method.
However, if the characteristics of all three methods are examine,
you'll find that Java's memory method characteristics has more in
common with the C++ pointers, and have very little in common with the
concrete method.
In C++, when a concrete variable is declared, its constructor is always
called
When a reference variable is declared, a constructor is never called.
When a pointer variable is declared a constructor is called only if it
is initialized by pointing to a NEW-operator.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, a concrete variable must be initialized to address valid
memory.
A reference variable must also be initialized to address valid memory.
A pointer may be initialized to point to nothing at all, or to point to
valid memory.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, a concrete variable (once initialized) can never change the
memory it's addressing.
A reference variable (once initialized) can never change the memory
it's addressing.
A pointer can change what it's pointing to at any time.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, you cannot assign a concrete variable to new operator.
A reference variable cannot be directly assigned to a new operator.
A pointer can be directly assigned to a new operator.
In this characteristic, Java address modal matches only the pointer
modal.
In C++, pointer arithmetic cannot be performed on a concrete type.
Pointer arithmetic cannot be performed on a reference type.
Pointer arithmetic can of course be performed on a pointer variable.
This is where Java memory modal and C++ pointers differ. You cannot
perform pointer arithmetic on a Java address method.
Other then pointer arithmetic, Java's memory modal is very much a C++
pointer memory modal.
So if a Java book really wanted to be more accurate, it would say:
"Java only uses pointers, and Java pointers lack the ability to
perform pointer arithmetic."
OK, frag away!