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 :)
谢谢:)
7 个解决方案
#1
13
A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be 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 and pointers - the latter being used in "unsafe" code).
Java没有这样的指针(不像c#那样有引用和指针——后者在“不安全”代码中使用)。
#2
9
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没有指针。但是如果你尝试使用一个空引用你会得到一个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++“引用”概念更接近。即使您想直接使用内存地址,也不能直接使用它,但您可以使用相同的方式。
#3
3
No, Java does not have pointers. The fundamental concepts in Java are "values" vs "references".
不,Java没有指针。Java中的基本概念是“值”vs“引用”。
#4
2
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不允许对引用或内存位置进行控制。
#5
1
A reference is a pointer that you can't normally see the value of (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 new operator, or from another reference (which is syntactically a simple reference-valued expression).
引用是一个指针,你通常不能看到它的值。,内存地址)。唯一允许的操作是设置它(从另一个引用),并通过它引用到被引用的对象。它可以从引用值表达式(如新运算符)或另一个引用(从语法上来说,它是一个简单的引用值表达式)设置。
#6
1
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".
指针的类型不那么强,引用的类型更强。我的意思是,在引用的情况下,代码非常简单,而在指针的情况下,代码稍微不那么正式。你需要利用reinterpret_cast这类东西来使用指针cast另一方面,引用支持简单的cast,就像C中的原始类型cast一样。
-
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.
指针是不安全的,引用是安全的。如果您在两个选择引用之间有一个选项。在c#这样的语言中,你有这种*。
-
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++.
指针存在于C/ c++中,引用存在于Java中。不要将C/ c++中的引用与Java中的引用混淆。C/ c++中的引用是C/ c++中指针rvalue的同义词。
#7
-1
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.
指针只包含地址但参考并不包含对象的地址如果我们说坦白说那地址分配到索引或者我们说可以散列码和案例代码的引用变量如果我们将看到的内容引用变量后,它开始与类名@一些十六进制代码。这些nos不是地址,它是一个索引值或哈希代码。
second point we can not perform any Arithmetic operations on values the Content of reference value
第二点,我们不能对引用值的内容进行任何算术运算
#1
13
A reference is sort of like a pointer that you can't do arithmetic on... although it's more opaque. While the underlying bits may be 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 and pointers - the latter being used in "unsafe" code).
Java没有这样的指针(不像c#那样有引用和指针——后者在“不安全”代码中使用)。
#2
9
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没有指针。但是如果你尝试使用一个空引用你会得到一个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++“引用”概念更接近。即使您想直接使用内存地址,也不能直接使用它,但您可以使用相同的方式。
#3
3
No, Java does not have pointers. The fundamental concepts in Java are "values" vs "references".
不,Java没有指针。Java中的基本概念是“值”vs“引用”。
#4
2
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不允许对引用或内存位置进行控制。
#5
1
A reference is a pointer that you can't normally see the value of (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 new operator, or from another reference (which is syntactically a simple reference-valued expression).
引用是一个指针,你通常不能看到它的值。,内存地址)。唯一允许的操作是设置它(从另一个引用),并通过它引用到被引用的对象。它可以从引用值表达式(如新运算符)或另一个引用(从语法上来说,它是一个简单的引用值表达式)设置。
#6
1
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".
指针的类型不那么强,引用的类型更强。我的意思是,在引用的情况下,代码非常简单,而在指针的情况下,代码稍微不那么正式。你需要利用reinterpret_cast这类东西来使用指针cast另一方面,引用支持简单的cast,就像C中的原始类型cast一样。
-
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.
指针是不安全的,引用是安全的。如果您在两个选择引用之间有一个选项。在c#这样的语言中,你有这种*。
-
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++.
指针存在于C/ c++中,引用存在于Java中。不要将C/ c++中的引用与Java中的引用混淆。C/ c++中的引用是C/ c++中指针rvalue的同义词。
#7
-1
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.
指针只包含地址但参考并不包含对象的地址如果我们说坦白说那地址分配到索引或者我们说可以散列码和案例代码的引用变量如果我们将看到的内容引用变量后,它开始与类名@一些十六进制代码。这些nos不是地址,它是一个索引值或哈希代码。
second point we can not perform any Arithmetic operations on values the Content of reference value
第二点,我们不能对引用值的内容进行任何算术运算