java Java中的指针和引用变量有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7436581/
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
What is the difference between a pointer and a reference variable in Java?
提问by i love stackoverflow
My Java book explains that to use objects, we can assign them to reference variables. How is that different from a pointer to an object? Does Java have pointers?
我的 Java 书解释说要使用对象,我们可以将它们分配给引用变量。这与指向对象的指针有何不同?Java 有指针吗?
Thanks :)
谢谢 :)
回答by Jon Skeet
A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits maybe an address in virtual memory, they don't have to be. They're just a way of getting to an object (or representing the null value). So while they're not exactly the same, if you're used to thinking of a pointer as "a way of identifying an object or navigating to it" (in some sense) then yes, those thoughts apply to references too.
引用有点像指针,您无法对其进行算术运算……尽管它更不透明。虽然底层位可能是虚拟内存中的地址,但它们不一定是。它们只是获取对象(或表示空值)的一种方式。因此,虽然它们并不完全相同,但如果您习惯于将指针视为“识别对象或导航到对象的一种方式”(在某种意义上),那么是的,这些想法也适用于引用。
Java doesn't have pointers as such (unlike, say, C# which has references andpointers - the latter being used in "unsafe" code).
Java 本身没有指针(与 C# 不同,C# 具有引用和指针——后者在“不安全”代码中使用)。
回答by wberry
The terms "reference" and "pointer" are basically equivalent. Much of the literature I've seen about the basics of Java claims that Java has no pointers. But if you try to use a null
reference you get a NullPointerException
. So it's all semantics.
术语“引用”和“指针”基本上是等价的。我看过的很多关于 Java 基础的文献都声称 Java 没有指针。但是,如果您尝试使用null
引用,则会得到NullPointerException
. 所以这都是语义。
(The real difference is, in C or C++ the term "pointer" strictly means an integer that happens to be the memory address of some data. Whereas in Java the term "reference" more closely matches the C++ "reference" concept. You can't work with the memory address directly even if you want to, but you use it the same way.)
(真正的区别在于,在 C 或 C++ 中,术语“指针”严格表示恰好是某些数据的内存地址的整数。而在 Java 中,术语“引用”更接近于 C++“引用”概念。您可以即使您愿意,也不能直接使用内存地址,但您可以以相同的方式使用它。)
回答by conapart3
Cat x = new Cat();
This line creates a Cat object in memory and stores a reference to it in x.
这一行在内存中创建了一个 Cat 对象,并将对它的引用存储在 x 中。
x now contains a reference to the Cat object, but if you were to do:
x 现在包含对 Cat 对象的引用,但如果您要这样做:
x = x + 1;
This would not give the next memory address like in C, but would give a compiler error. Java doesn't allow control to the reference or memory location.
这不会像在 C 中那样给出下一个内存地址,但会给出编译器错误。Java 不允许控制引用或内存位置。
回答by Nagabhushan Baddi
If we think of references or pointers as a kind of means to access or handle the object in memory, they are much the same. However, they are different in terms of implementation. Following are the differences:
如果我们将引用或指针视为访问或处理内存中对象的一种手段,它们是大致相同的。但是,它们在实现方面是不同的。以下是差异:
Pointers support pointer arithmetic and this makes them a kind of unsafe. Because they provide the programmer with an access level that is much more than needed. However, references provide an handle to the object through a much more abstract hided implementation.
Pointers are less strongly typed and references are more strongly typed. By this I mean the code is much simple in case of references and slightly less formal in case of pointers. You need to make use of reinterpret_cast kind of things to use pointers casts and on the other hand, references support easy casts of the kind of primitive kind of casts in "C".
Pointers are unsafe and references are safe. If you have an option between the two select references. In languages like C#, you have this freedom.
Pointers exists in C/C++ and references exist in Java. Do not confuse references in C/C++ with references in Java. References in C/C++ is a kind of synonym used for the rvalue of a pointer in C/C++.
指针支持指针运算,这使得它们有点不安全。因为它们为程序员提供了远远超出需要的访问级别。但是,引用通过更抽象的隐藏实现提供对象的句柄。
指针不是强类型的,而引用是强类型的。我的意思是代码在引用的情况下非常简单,而在指针的情况下则稍微不那么正式。您需要利用 reinterpret_cast 类型的东西来使用指针转换,另一方面,引用支持“C”中原始类型转换的简单转换。
指针是不安全的,而引用是安全的。如果您在两个选择参考之间有一个选项。在像 C# 这样的语言中,您拥有这种自由。
C/C++ 中存在指针,Java 中存在引用。不要将 C/C++ 中的引用与 Java 中的引用混淆。C/C++ 中的引用是 C/C++ 中指针右值的同义词。
回答by Ed Staub
A reference is a pointer that you can't normally see the valueof (i.e., the memory address). The only operations allowed are to set it (from another reference) and to reference through it to the referred-to object. It can be set from a reference-valued expression, such as the newoperator, or from another reference (which is syntactically a simple reference-valued expression).
引用是一个指针,你通常看不到它的值(即内存地址)。唯一允许的操作是设置它(从另一个引用)并通过它引用被引用的对象。它可以从引用值表达式(例如new运算符)或另一个引用(在语法上是一个简单的引用值表达式)中设置。
回答by Kerrek SB
No, Java does not have pointers. The fundamental concepts in Java are "values" vs "references".
不,Java 没有指针。Java 中的基本概念是“值”与“引用”。
回答by uptoyou
In terms of Java syntax - it's true, that Java doesn't have pointers, but to clarify situation - Java Runtimehaspointers.
Every time GCoccurs, there is a big chance, that your object stored in the heapwill physically change it's memory address. This is achievable only by using what is really called Pointer. As you come from C++
world - you know reference restrictions. But it doesn't mean that JVM doesn't use references =).
在 Java 语法方面 - Java 确实没有指针,但为了澄清情况 - Java 运行时有指针。每次GC发生时,存储在堆中的对象很有可能会物理改变它的内存地址。这只能通过使用真正称为Pointer 的东西来实现。当您来自C++
世界时 - 您知道参考限制。但这并不意味着 JVM 不使用引用 =)。
For further information take a look at Ordinary Object Pointer
有关更多信息,请查看普通对象指针
So, as you have brief understanding of core JVM memory management, you can consider Java Referenceterm as Pointerat runtime.
因此,由于您对核心 JVM 内存管理有了简要的了解,您可以将Java 参考术语视为运行时的指针。
No, there is no pass by referenceat all. Only pass by value, since the real Java method argument is a copy of Java Reference, therefore - another Pointer.
不,根本没有通过引用传递。仅按值传递,因为真正的 Java 方法参数是Java Reference的副本,因此 - 另一个Pointer。
So, in syntax usage way - Java Referenceis closer to C++ Reference, but in real Runtime world it's close to Pointer.
因此,在语法使用方式上 - Java Reference更接近C++ Reference,但在实际运行时世界中它接近Pointer。
P.s. for more clarification it's better to read more articles about Ordinary Object Pointeror take a loot at oop.hpp.
Ps 要获得更多说明,最好阅读更多关于普通对象指针的文章或在oop.hpp 上搜一下。
回答by Vivek Kumar
pointer only contain the address but Reference does not contains address if we say frankly then address of the object is assigned to the index or we say can hash code and case code is given to the reference variable if we will see the content of the reference variable it starts with class Name @ and after it some Hexadecimal Code. These nos are not address it is a index value or hash code.
指针只包含地址,但引用不包含地址,如果我们坦率地说,对象的地址被分配给索引,或者我们说可以散列代码和大小写代码给引用变量,如果我们将看到引用变量的内容它以类名称@ 开头,然后是一些十六进制代码。这些编号不是地址,它是索引值或哈希码。
second point we can not perform any Arithmetic operations on values the Content of reference value
第二点我们不能对值执行任何算术运算参考值的内容