He's absolutely right. Java "references" are actually called
references, but they are more similar to C/C++ style pointers. In fact
they can be pointed to NULL (null in Java).
Java programmers are more used to dealing with pointers than
references. The "new" operator in C++ returns a pointer to some memory
in which that object, structure, union, enum, or primitive type is
stored:
I.E:
Object* myObj = new Object();
Seems vaguely similar to the Java syntax:
Object myObj = new Object();
A reference is instead a handle directly to some memory that can't be
reassigned, and is automaticly dereferenced. It's more like an alias
to an already existing variable.
Object myObj; //Allocates an object on the stack.
Object& myObjRef = myObj; //Makes an alias to myObj called "myObjRef."
myObj.doSomething();
myObjRef.doSomething(); //Does exactly the same thing as above.
Another example would be:
int myInt = 5;
int& myIntRef = myInt; // Alias to myInt;
myInt = 6;
printf("%d\n", myIntRef); // Prints out 6.
The basic difference is, if you use a pointer you actually are just
using a handle to an object. You can reassign that handle at any time
to some other Object. It's basically just a variable that holds a
memory address and is associated with a type.
This is really a big deal with a function:
int bob(Object* myPtr, Object& myRef)
{
myPtr = NULL; //The object passed isn't effected.
myRef = NULL; //The actual object passed is changed, not just in
this scope.
}
I personaly believe references are superfluous, and hide the fact that
an arguement you've passed may be altered in the function you're
passing to. I'd limit my use of them if I were you. Pointers are a
great deal more robust, easy to use and understand, and it's clear
what is going on; nothing is kept secret from the programmer.
-Jason Thomas.
op*****@yahoo.com (Samuel Barber) wrote in message news:<37**************************@posting.google. com>...
Dave Rahardja <as*@me.com> wrote in message news:<v4********************************@4ax.com>. .. Reading your other posts, I assume you are a Java programmer.
References are what Java programmers are used to.
No. Java does not have C++ style references. Java has implicit
pointers. They differ from C/C++ pointers in that you can't do pointer
arithmatic, or unsafe casts.
Sam