Java has no pointers; Java has links.
This is a thin point, but the pointer has additional operations that you can (or cannot) use; the link does not contain these operations, because operations may be unsafe.
For example, if you pointed to the first element of an array, for example:
int squares[] = {1, 4, 9, 16, 25, 36, 49}; int* intPointer = squares;
you can dereference the pointer and get the value "1", but you can also:
intPointer++
and after that, when you look for the pointer, you get the value "4". Second
intPointer++;
will, when dereferenced, give you the value "9". This is because the ++ operation moves the pointer one “unit” forward in memory.
The problem arises due to flaws in the C / C ++ type checking system (C ++ must support compatibility with C, so it allows the same problems). The pointer stores the address in memory, and the ++ operation adds the appropriate number of bytes to the address. In many ++ ++ systems, int int adds four bytes, but if the pointer was a char ++ ing pointer, it should only add one byte. Please note that since the pointer's base data type is an address in memory, the following law (but not recommended):
char* charPointer = squares; charPointer++; void* voidPointer = squares; voidPointer++;
Because pointers are addresses in memory, they can represent (correctly) any bit of memory in the computer, but they are only dereferenced correctly when the underlying data captures the type and alignment of the pointer. For pointers that are not controlled by a lot of code to make them safe, this means that you can reject the data type (or alignment) of the desired information, and dereferencing can end in disaster. Attempting to fix this problem with custom code tends to slow down one pointer so much that you notice performance problems, and it opens the door to adding errors to your custom pointer control code.
Java accomplishes all these problems by returning a link. The link does not refer to any location in memory; Java maintains an internal pointer-reference table. This table takes a link and returns the data associated with it, wherever this data is in memory. This slows down code execution, because for each "dereferencing" one search is performed, one search in the lookup table, one in the computer's memory.
The big advantage of Java using references is that memory can move without breaking potential pointer addresses. In program C, if you move data to a new memory location, it is very difficult to determine if any other part of the program has a data pointer. If the crossed out pointer is dereferenced after moving the memory, the program will access the damaged data, and, as a rule, a failure will have a drawback.
The ability to move memory in a running program allows programs to easily recycle memory. Any program that does not need chunks of memory can free up unused memory, but this creates memory openings for unused memory between chunks of used memory. Internal computers use memory pages that are quite large. If a rarely used memory page may contain several used bits moved to another page, then you can free the memory page. This increases the density of data in memory, improving cache performance. Sometimes this leads to improved performance, which can be quite dramatic.
The Java Garbage Collector exploits the use of links, temporarily blocking access to data for a set of links. During this access lock, it moves data around (to compress it). After locking, the link to the address table contains the new memory addresses. Since the "functional" level of code never knew the addresses in the first place, this operation will not disrupt the running Java program.