Pointers in Java

前端 未结 9 1978
梦毁少年i
梦毁少年i 2020-12-05 16:28

C++ supports pointers whereas Java does not. But when many programmers questioned how you can work without pointers, the promoters began saying \"Restricted pointers.” So we

相关标签:
9条回答
  • 2020-12-05 16:51

    When people say that Java doesn't support pointers, they are practicing newspeak. What Java calls references correspond exactly to what has always been known as pointers in the past.

    What Java doesn't support is pointer arithmetic. Which is something else entirely; as far as I know, C and its descendents are the only typed languages which support pointer arithmetic. Pascal and Modula-2, for example, have "pointers", described as pointers in their specifications, but these pointers have a semantic far closer to that of Java references; they don't allow pointer arithmetic.

    0 讨论(0)
  • 2020-12-05 16:52

    Java has pointers. That's why it has a NullPointerException. It just doesn't have pointer math. Any reference to an object is actually a pointer, which is why it can be null. That said, there are plenty of useful programming languages which don't have pointers, so anyone who thinks that pointers are necessary for programming has a very narrow view of programming languages.

    0 讨论(0)
  • 2020-12-05 16:59

    First, you need to understand "restricted pointers". Excerpt from Wikipedia:

    One major problem with pointers is that as long as they can be directly manipulated as a number, they can be made to point to unused addresses or to data which is being used for other purposes. Many languages, including most functional programming languages and recent imperative languages like Java, replace pointers with a more opaque type of reference, typically referred to as simply a reference, which can only be used to refer to objects and not manipulated as numbers, preventing this type of error. Array indexing is handled as a special case.

    What it means is that in Java, you can't add or subtract to a pointer since memory management is done by the JVM itself.

    Java adopted reference. References have types, just like in C, and they're typesafe as these reference cannot be interpreted as raw address and unsafe conversion is not allowed.

    0 讨论(0)
  • 2020-12-05 16:59

    Let me be acid: Java don't have pointers because it's designers decided to call them differently. In fact they moved everything on the heap so that everything is managed by a pointer, then, since no direct reference existed anymore, canceled the "." and renamed "->" as "."

    0 讨论(0)
  • 2020-12-05 17:00

    Another important different between Java and C/C++ is that references are an index to an object. Whereas in C/C++ a pointer is an address in memory.

    In the 32-bit JVM, they are the same thing, however in the 64-bit JVM they are not. Where you will notice this difference is that for heap sizes less than 32 GB, references are still 32-bit (even in a 64-bit JVM) This is because objects are allocated on an 8 byte boundary, so the index can refer to up to 32 GB of memory (4 G * 8 bytes)

    In a 64-bit C/C++ programs, a pointer needs to be able to reference every byte even though memory allocation is on a 16 byte boundary and so it is 64-bit in size (technically it should be possible to make it 32-bit for less than 4 GB of memory.)

    A smart pointer needs two underlying pointers (for a total of 16 bytes) but on gcc, the minimum allocation size for the reference count is 32 bytes (And then you have the size of the object you point to) The total size is 32 bytes + 16 bytes per pointer. c.f. 4 bytes per reference in Java. (8 bytes if you have 32+ GB of heap)

    In summary, a Java reference doesn't have to be the actual address or even the same size as a pointer. It certainly much smaller than a smart pointer.

    0 讨论(0)
  • 2020-12-05 17:03

    The Java language specification has this to say about the matter:

    Java Language Spec §4.3.1
    The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

    For those who fear delving into documentation, the presence of e.g. Java’s NullPointerException should be a strong indication that Java does have pointers.

    In short, the question is meaningless because it is based on a totally incorrect assumption that, quoting the OP, “Java does not pointers” – as proven above, that is technically bullshit.

    See also James Kanze’s answer.

    This answer can best be viewed as just supplying the necessary references to James’ answer.

    Cheers & hth.

    0 讨论(0)
提交回复
热议问题