Does Java have pointers?

If Java has no pointers, then what does the new keyword do in Java?

+55
java pointers
Apr 13 '10 at 12:12
source share
12 answers

As indicated, Java has links. How do they differ?

  • you cannot perform arithmetic or other similar operations on these
  • they do not point to the memory containing the object (i.e. they are not pointers by another name). The JVM has the right to move objects within the VM memory and most likely will do during garbage collection. However, references still point to this object, despite its movement in memory.

Therefore, they are not like C ++ links (pointing directly to an object). Perhaps the best name would be a handle.

+47
Apr 13 '10 at 12:17
source share

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.

+37
Apr 13 '10 at 13:13
source share

Java has pointers in the sense of variables that store references to data in memory. All object type variables in Java are pointers in this sense.

However, Java does not allow arithmetic to be performed on pointer values, as you would in a language such as C.

+12
Apr 13 '10 at 12:14
source share

new does (roughly) the following:

  • Find the adjacent free heap memory block equal to the size of the instance of the class being created, plus some space for accounting.
  • Zero space and remove it from the free list
  • Run the constructor
  • Return the link (not a pointer, as other posts explained) to the created instance.
+7
Apr 13 '10 at 12:40
source share

java.lang.NullPointerException

People told me that “he has no pointers” in an interview. I usually gave them some Java code and let them explain what was going on in this code:

 public class TestPointers { public static void main(String args[]) { Object p1, p2; p1 = new Object(); p2 = p1; p1 = null; System.out.println(p2); } } 
+3
Jun 13 '13 at 16:40
source share

There are pointers in Java that are known as " link ".

When people say that "Java has no pointers," they tend to confuse the concept of a pointer with the specific implementation and ability of pointers found in C and C.

In particular:

  • Java links cannot be set to an arbitrary address . Also, (standard) Pascal or Fortran pointers cannot.
  • Java references cannot point to a variable . Nor can (standard) Pascal pointers.
  • Java references do not support pointer arithmetic . Also not Pascal and Fortran pointers
  • Java references cannot point to parts of an object (for example, the third element of an array). Also can not Pascal pointers.

In addition, contrary to popular belief, a pointer is not necessarily an address . A pointer is usually implemented as an address, but there is no need to do this, not even in C or C ++.

+3
Aug 14 '16 at 12:19
source share

Java has links. Access to all objects is carried out by reference to their instances. You create a new instance with new , which returns a reference to the object.

Java references are not like C pointers; you cannot “look under the hood” in the raw memory that makes up the object.

+1
Apr 13 '10 at 12:15
source share

new in Java returns a reference for a newly created object.

0
Apr 13 '10 at 12:15
source share

new refund link. it has some similarities with pointers (if you go to a function, a link is passed, the same as with a pointer), but there is no arithmetic for pointers.

0
Apr 13 '10 at 12:15
source share

Java has no pointers. The "new" operator is used for a reference variable in java.

-one
Apr 13 '10 at 12:27
source share

Java does not support or permit pointers. (Or more correctly, Java does not support pointers that can be accessed and / or modified by the programmer.) Java cannot allow pointers because it will allow Java applets to break the firewall between the Java runtime and the host computer. (Remember that a pointer can be assigned any address in memory - even addresses that may be outside of the Java runtime system.)

-one
Apr 23 '16 at 9:36
source share

In Java, we come across certain keywords used as references, for example, this keyword is used to refer to variables of the same class. The new operator is used as a reference to an object.

-3
Apr 13 '10 at
source share



All Articles