Are there pointers in java

People who have learned c/c++ and then java often have this entanglement in their minds. Are there any pointers in java?
If you must choose one of yes or no, OK, there is no pointer in java.

So, what kind of entanglement is there between references in java and pointers in c/c++ (if not otherwise specified, pointers in the following refer to pointers in c/c++)?

Before we start, let's talk about c/c++ references. It is not the same thing as java references. C/c++ references are different names for the same piece of memory. The java reference refers to an object, and the reference itself also takes up 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. Operate on the object pointed to by the pointer: P- >getAge(); (*p).getAge();
3. Get 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 assigned.
5. Delete the space of new, delete p1;
6. Many other operations

So much is enough, let's take a look at what java references can do.
a. Point to an object, such as Person p = new Person...
b. Call the method of the object, 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 the java reference cannot point to a basic variable. .
For b, java reference and pointer are different. In this regard, java reference is more like a dereferenced pointer; or the. Operator in java is equivalent to the pointer -> operation to some extent If this is the explanation, we can no longer dereference the java reference.
For c and 4, they are similar.

In addition, we can't get the address of the java reference, we can't delete, there are many more...

In summary, it is stolen that a reference to java can be regarded as a pointer with limited functionality (or a pointer that is castrated). On the other hand, the function is so limited, can it still be called a pointer? After understanding the difference and connection between pointers and java references, do you still need to entangle the question of "is there a pointer in java"!

Guess you like

Origin blog.csdn.net/guorui_java/article/details/109366672