java Java中的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7480783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pointers in Java
提问by Nikunj Patel
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 can say Java supports Restricted pointers?
C++ 支持指针,而 Java 不支持。但是当许多程序员质疑如何在没有指针的情况下工作时,发起人开始说“受限指针”。那么我们可以说Java支持受限指针吗?
回答by Joachim Sauer
The terminology is quite fuzzy here.
这里的术语非常模糊。
Java supports what it calls "references". References act a lotlike pointers in C/C++-like languages. They don'tact the same way "references" work in those languages.
Java 支持它所谓的“引用”。引用行为 很多像在C / C ++指针-喜欢的语言。他们不会采取同样的方式“引用”,在这些语言的工作。
The majordifferences between a pointer in C and a reference in Java are:
C 中的指针和 Java 中的引用之间的主要区别是:
- You can't do pointer arithmetic in Java (i.e. you can't "add" or "subtract" from a Java reference, you can onlydereferencere 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.
- 您不能在 Java 中进行指针运算(即您不能从 Java 引用中“添加”或“减去”,您只能取消引用它或将它与另一个引用进行比较)。
- 您不能将其强制转换为不兼容的类型:Java 是强类型安全的,您不能将内存中的字节“重新解释”为其他对象。
For someuses 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).
对于指针的某些用途,这没有实际影响(例如,链表在两种语言中的工作方式几乎相同),对于其他人,差异非常大(C 中的数组只是花哨的指针算术,在 Java 中它们的工作方式完全不同)。
So in a way Java references could be called "restricted pointers".
所以在某种意义上,Java 引用可以被称为“受限指针”。
... a programming language data type whose value refers directlyto (or "points to") another value
... 一种编程语言数据类型,其值直接引用(或“指向”)另一个值
Emphasis mine. According to this strictdefinition, Java doesn't have pointers.
强调我的。根据这个严格的定义,Java 没有指针。
The more general referenceis the superclass of pointers, but also contrains more abstract things like file handles or even URLs.
更一般的引用是指针的超类,但也包含更抽象的东西,如文件句柄甚至 URL。
回答by Peter Lawrey
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.
Java 和 C/C++ 之间的另一个重要区别是引用是对象的索引。而在 C/C++ 中,指针是内存中的地址。
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)
在 32 位 JVM 中,它们是相同的东西,但是在 64 位 JVM 中它们不是。您会注意到这种差异的地方在于,对于小于 32 GB 的堆大小,引用仍然是 32 位的(即使在 64 位 JVM 中)这是因为对象是在 8 字节边界上分配的,因此索引可以向上引用到 32 GB 内存(4 G * 8 字节)
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.)
在 64 位 C/C++ 程序中,即使内存分配在 16 字节边界上,指针也需要能够引用每个字节,因此它的大小是 64 位(从技术上讲,应该可以将其设为 32-位小于 4 GB 的内存。)
A smart pointerneeds 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)
甲智能指针需要两个底层指针(总共16个字节),但是,GCC,用于参考计数的最小分配大小为32个字节(然后你有对象的大小您指向)的总大小为32个字节+ 每个指针 16 个字节。参见 Java 中每个引用 4 个字节。(如果您有 32+ GB 的堆,则为 8 个字节)
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.
总之,Java 引用不必是实际地址,甚至不必与指针大小相同。它肯定比智能指针小得多。
回答by Buhake Sindi
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.
指针的一个主要问题是,只要它们可以作为数字直接操作,它们就可以指向未使用的地址或用于其他目的的数据。许多语言,包括大多数函数式编程语言和最近的命令式语言,如 Java,用更不透明的引用类型替换指针,通常简称为引用,它只能用于引用对象而不能作为数字进行操作,从而防止了这种情况错误类型。数组索引作为特殊情况处理。
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 中,您不能对指针进行加减运算,因为内存管理是由 JVM 本身完成的。
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.
Java采用参考。引用有类型,就像在 C 中一样,它们是类型安全的,因为这些引用不能解释为原始地址,并且不允许不安全的转换。
回答by James Kanze
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.
当人们说 Java 不支持指针时,他们是在练习新话。Java 所称的引用与过去一直称为指针的内容完全对应。
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 不支持的是指针算法。这完全是另一回事;据我所知,C 及其后代是唯一支持指针算术的类型语言。例如,Pascal 和 Modula-2 有“指针”,在它们的规范中被描述为指针,但这些指针的语义与 Java 引用的语义非常接近;他们不允许指针算术。
回答by Kaiser Keister
Java actually doeshave pointer math. It comes with sun.misc.Unsafe. However, you have to manage the memory yourself - be careful.
Java 实际上确实有指针数学。它带有 sun.misc.Unsafe。但是,您必须自己管理内存 - 小心。
回答by Cheers and hth. - Alf
The Java language specificationhas this to say about the matter:
在Java语言规范是这样说的这件事:
Java Language Spec §4.3.1
The reference values (often just references) are pointersto these objects, and a special null reference, which refers to no object.
Java 语言规范 §4.3.1
引用值(通常只是引用)是指向这些对象的指针,以及一个特殊的空引用,它不引用任何对象。
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.
对于那些害怕钻研文档的人来说,例如 Java 的存在NullPointerException
应该是一个强有力的迹象,表明 Java 确实有指针。
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.
简而言之,这个问题毫无意义,因为它基于一个完全错误的假设,引用 OP,“Java 没有指针”——如上所述,这在技术上是胡说八道。
See also James Kanze’s answer.
另请参阅James Kanze 的回答。
This answer can best be viewed as just supplying the necessary references to James’ answer.
这个答案最好被视为只是为詹姆斯的答案提供了必要的参考。
Cheers & hth.
干杯& hth。
回答by Keith Irwin
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.
Java 有指针。这就是它有一个 NullPointerException 的原因。它只是没有指针数学。任何对对象的引用实际上都是一个指针,这就是它可以为空的原因。也就是说,有很多有用的编程语言没有指针,所以任何认为指针是编程所必需的人对编程语言的看法非常狭隘。
回答by Emilio Garavaglia
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 "."
让我说酸:Java 没有指针,因为它的设计者决定以不同的方式调用它们。事实上,他们移动了堆上的所有内容,以便所有内容都由一个指针管理,然后,由于不再存在直接引用,取消了“。” 并将“->”重命名为“。”
回答by user1122294
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.
指针只是一种实现可变返回的方法。它们对于 effektiv 工作并没有真正的重要性。它更易于使用,并且与指针相比,您可以更好地理解代码。在 c 中的大多数源代码中,我看到的 &/*//-> 比其他东西更多,如果您需要它,您曾经看过。