People who have learned c/c ++ and then learn java often have this tangle in mind. Is there any pointer in java?
If you must select either yes or no, OK and java has no pointer.
So, the reference in java and the pointer in c/c ++ (unless otherwise stated, the pointer in the following article refers to the pointer in c/c ++) what is the tangle?
Before starting, let's talk about the reference of c/c ++. It is totally different from the reference of java, and the reference of c/c ++ is the same memory name. Java references point to an object, which also occupies memory.
First, list some common operations that can be performed on pointers:
1. point to an object, such as Person * p = new Person ....; Int * iv = new int ....
2. perform operations on the object indicated by the pointer: P-> getAge (); (* p). getAge ();
3. Obtain the address value stored in the pointer.
4. Point the pointer to another object: Person * p1 = new Person ...; P = p1; however, * p = p1 cannot be used to assign values.
5. delete the new space, delete p1;
6. Other operations
This is enough. Let's take a look at the operations that java references can do.
A. point to an object, such as Person p = new Person...
B. Call the object method, p. getAge ();
C. Point to another object, Person p1 = new Person ...; P = p1;
OK, that's all.
For a and 1, their functions are similar, but java references cannot point to a basic variable ..
For B, java references and pointers are different. In this regard, java reference is more like a pointer to be referenced (dereference); or in java. to some extent, the operator is equivalent to the pointer-> operator. If so, we can no longer reference the java reference.
C and 4 are similar.
In addition, we cannot obtain the address of the Reference Memory in java, we cannot delete, there are many ......
In summary, I think that java references can be regarded as a pointer with limited functionality (or a castrated pointer ). On the other hand, can a function still be called a pointer if it is so limited? After learning about the differences and links between pointers and java references, do you still need to find out if there are any pointers in java!