Java 有指针吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2629357/
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
Does Java have pointers?
提问by aruna
If Java does not have pointers, then what does the the new
keyword do in Java?
如果 Java 没有指针,那么new
关键字在 Java 中有什么作用?
回答by Syntactic
Java has pointers in the sense of variables that store references to data in memory. All variables of Object types in Java are pointers in this sense.
Java 具有变量意义上的指针,用于存储对内存中数据的引用。在这个意义上,Java 中所有 Object 类型的变量都是指针。
However, the Java language does not allow arithmetic operations on the values of pointers, like you'd be able to do in a language like C.
但是,Java 语言不允许对指针的值进行算术运算,就像在 C 之类的语言中一样。
回答by codaddict
new
in Java returns a referenceto the newly created object.
new
在 Java 中返回对新创建对象的引用。
回答by Andrey
new returns reference. it has some similarities with pointers (if you pass to function, reference is passed, same as with pointer), but there is no pointer arithmetics.
新的退货参考。它与指针有一些相似之处(如果传递给函数,则传递引用,与指针相同),但没有指针算术。
回答by unwind
Java has references. All objects are accessed through having references to their instances. You create a new instance using new
, which returns a reference to the object.
Java 有参考。所有对象都通过引用它们的实例来访问。您使用 来创建一个新实例new
,它返回对对象的引用。
Java references are not like pointers in C, you cannot "look under the hood" at the raw memory that makes up the object.
Java 引用不像 C 中的指针,您不能“深入了解”构成对象的原始内存。
回答by Brian Agnew
As pointed out, Java has references. How are these different ?
正如所指出的,Java 有引用。这些有什么不同?
- you can't perform arithmetic or other such operations on these
- they do not point to the memory containing the object(i.e. they are not pointers by another name). The JVM is at liberty to move objects around within the VM memory, and most likely will do during garbage collection. The references however still point to that object, despite its movement within memory.
- 您不能对这些执行算术或其他此类操作
- 它们不指向包含对象的内存(即它们不是另一个名称的指针)。JVM 可以自由地在 VM 内存中移动对象,并且很可能会在垃圾收集期间这样做。然而,引用仍然指向那个对象,尽管它在内存中移动。
So they're not like C++ references (pointing directly to an object). Perhaps a better name would be handle.
所以它们不像 C++ 引用(直接指向一个对象)。或许一个更好的名字是handle。
回答by Venkat
Java Does not have Pointers. The operator "new" is used to the reference variable in java.
Java 没有指针。运算符“new”用于java中的引用变量。
回答by user315459
In java we come across certain keywords used as reference for example this
keyword is used to refer the variables of same class. The operator new is used as reference to an object.
在java中我们会遇到一些用作引用的this
关键字,例如关键字用于引用同一类的变量。运算符 new 用作对对象的引用。
回答by thecoop
new
does (roughly) the following:
new
执行(大致)以下操作:
- Find a contiguous free block of heap memory equal to the instance size of the class you're creating, plus some space for bookkeeping
- Zero said space & remove it from the free list
- Run the constructor
- Return a reference (NOT a pointer, as other posts have explained) to the created instance.
- 找到一个连续的空闲堆内存块,等于你正在创建的类的实例大小,加上一些用于簿记的空间
- 零表示空间并将其从空闲列表中删除
- 运行构造函数
- 返回对创建的实例的引用(不是指针,正如其他帖子所解释的那样)。
回答by Edwin Buck
Java doesn't have pointers; Java has references.
Java 没有指针;Java 有参考。
It's a fine point, but a pointer has extra operations that you may (or may not) typically use; a reference lacks these operations because the operations may be unsafe.
这是一个很好的观点,但是指针具有您可能(或可能不)通常使用的额外操作;引用缺少这些操作,因为这些操作可能不安全。
For example, if you use a pointer to index the first element of an array like so:
例如,如果您使用指针来索引数组的第一个元素,如下所示:
int squares[] = {1, 4, 9, 16, 25, 36, 49};
int* intPointer = squares;
you may want to dereference the pointer and get the value "1", but you may also:
您可能想要取消引用指针并获得值“1”,但您也可能:
intPointer++
and after you do that, when you dereference the pointer you will get the value "4". A second
这样做之后,当您取消引用指针时,您将获得值“4”。一秒
intPointer++;
will, when dereferenced, give you the value "9". This is because the ++ operation moves the pointer one "unit" ahead in memory.
将在取消引用时为您提供值“9”。这是因为 ++ 操作将指针在内存中向前移动一个“单位”。
The issue comes from the weaknesses in the C / C++ typechecking system (C++ must maintain compatibilty with C, so it allows the same issues). The pointer stores an address in memory and the ++ operation adds the appropriate number of bytes to the address. On many systems ++ing an int adds four bytes, but if the pointer was a char pointer ++ing it should only add one byte. Note that since the underlying data type of a pointer is an address in memory, the following is legal (but not recommended):
问题来自 C/C++ 类型检查系统的弱点(C++ 必须保持与 C 的兼容性,因此它允许相同的问题)。指针在内存中存储一个地址,++ 操作将适当数量的字节添加到地址中。在许多系统 ++ing 一个 int 增加四个字节,但如果指针是一个字符指针 ++ing 它应该只添加一个字节。请注意,由于指针的基础数据类型是内存中的地址,因此以下是合法的(但不推荐):
char* charPointer = squares;
charPointer++;
void* voidPointer = squares;
voidPointer++;
Since pointers are addresses in memory, they might represent (correctly) any bit of memory in the computer, but they are only properly dereferenced when the underlying data maches the type and alignment of the pointer. For pointers that aren't managed by lots of code to make them safe, this means you might stray off the data type (or alignment) of the desired information and a dereference might end in disaster. Attempting to fix this issue with custom code tends to slow down one pointers badly enough that you notice performance issues, and it opens the doors for adding errors in the custom "pointer management" code.
由于指针是内存中的地址,因此它们可能(正确地)表示计算机中的任何内存位,但只有当底层数据匹配指针的类型和对齐方式时,它们才会被正确取消引用。对于未由大量代码管理以使其安全的指针,这意味着您可能会偏离所需信息的数据类型(或对齐方式),并且取消引用可能以灾难告终。尝试使用自定义代码解决此问题往往会严重减慢一个指针的速度,以至于您会注意到性能问题,并为在自定义“指针管理”代码中添加错误打开了大门。
Java side steps all of these issues by returning a reference. A reference does not refer to any location in memory; Java maintains an internal "reference to pointer" table. This table takes the reference and returns the data associated with it, wherever that data may reside in memory. This slows down code execution, because two lookups are done for each "dereferencing", one lookup in the reference table, one in the machine's memory.
Java 端通过返回引用来解决所有这些问题。引用不引用内存中的任何位置;Java 维护一个内部的“对指针的引用”表。该表获取引用并返回与其关联的数据,无论该数据可能驻留在内存中的何处。这会减慢代码执行速度,因为每次“取消引用”都会执行两次查找,一次在引用表中查找,一次在机器内存中查找。
A big advantage of Java using references is that the memory can be moved around without breaking the would-be pointer addresses. In a C program, if you move data into a new memory location, it is very difficult to know whether some other part of the program has a pointer to the data. Should a stale pointer be dereferenced after the memory is moved, the program will be accessing corrupt data, and typically a crash will be shortcoming.
Java 使用引用的一大优点是可以在不破坏可能的指针地址的情况下移动内存。在 C 程序中,如果将数据移动到新的内存位置,则很难知道程序的其他部分是否有指向数据的指针。如果在移动内存后取消引用陈旧的指针,程序将访问损坏的数据,通常会导致崩溃。
Ability to move the memory around in a running program allows programs to easily recycle memory. Any program which doesn't need chunks of memory can release the unused memory, but this creates memory holes of unused memory in between chunks of used memory. Internally computers use pages of memory, which are quite large. If a sparsely used page of memory could have the few used bits moved into another page, then a page of memory can be freed. This increases the density of data to memory, improving cache performance. Sometimes this translates into performance improvements that can be quite dramatic.
在正在运行的程序中移动内存的能力允许程序轻松回收内存。任何不需要内存块的程序都可以释放未使用的内存,但这会在已用内存块之间产生未使用内存的内存孔。计算机内部使用相当大的内存页。如果一个很少使用的内存页面可以将少数使用的位移动到另一个页面中,那么可以释放内存页面。这增加了数据到内存的密度,提高了缓存性能。有时,这会转化为非常显着的性能改进。
Java's Garbage Collector takes advantage of the use of references by temporarily blocking access to the data for a set of references. During that blockage of access, it moves the data around (to compact it). After the blockage, the reference to address table has the new memory addresses. Since the "functional" layer of the code never knew the addresses in the first place, this operation will not break a running Java program.
Java 的垃圾收集器通过临时阻止对一组引用的数据的访问来利用引用的使用。在访问受阻期间,它会四处移动数据(以对其进行压缩)。阻塞后,对地址表的引用具有新的内存地址。由于代码的“功能”层从一开始就不知道地址,因此该操作不会破坏正在运行的 Java 程序。
回答by Edwin Buck
java.lang.NullPointerException
java.lang.NullPointerException
People told me "that java does not have pointers" in interviews. I usually gave them some java code and let them explain, what is happening in this code:
人们在采访中告诉我“java 没有指针”。我通常会给他们一些 java 代码,让他们解释一下,这段代码中发生了什么:
public class TestPointers {
public static void main(String args[]) {
Object p1, p2;
p1 = new Object();
p2 = p1;
p1 = null;
System.out.println(p2);
}
}