开发者

Pointers in Java

开发者 https://www.devze.com 2023-04-05 20:49 出处:网络
C++ supports pointers whereas Java does not. But when many programmers questioned how you can work without pointers, the promoters be开发者_如何学JAVAgan saying \"Restricted pointers.” So we can say

C++ supports pointers whereas Java does not. But when many programmers questioned how you can work without pointers, the promoters be开发者_如何学JAVAgan saying "Restricted pointers.” So we can say Java supports Restricted pointers?


The terminology is quite fuzzy here.

Java supports what it calls "references". References act a lot like pointers in C/C++-like languages. They don't act the same way "references" work in those languages.

The major differences between a pointer in C and a reference in Java are:

  • You can't do pointer arithmetic in Java (i.e. you can't "add" or "subtract" from a Java reference, you can only dereferencere it or compare it with another one).
  • You can't cast it to an incompatible type: Java is strongly type-safe, you can't "re-interpret" the bytes in memory as some other object.

For some uses of pointers this has no real effect (for example linked lists work pretty much the same in both languages), for others the difference is quite major (arrays in C are just fancy pointer arithmetic, in Java they work quite differently).

So in a way Java references could be called "restricted pointers".

Wikipedia defines a pointer as

... a programming language data type whose value refers directly to (or "points to") another value

Emphasis mine. According to this strict definition, Java doesn't have pointers.

The more general reference is the superclass of pointers, but also contrains more abstract things like file handles or even URLs.


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.


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.


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.


Java actually does have pointer math. It comes with sun.misc.Unsafe. However, you have to manage the memory yourself - be careful.


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.


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 "."


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.


Pointers are just a way to make mutible return. They not really importend for effektiv work. It is easier to use and you can understand the code better than with pointers. In the most source code in c, I see the more &/*/-> than other things and you have ever look if you need it.

0

精彩评论

暂无评论...
验证码 换一张
取 消