简体   繁体   English

C ++ / Java引用变量

[英]C++/Java reference variable

In Java, when I do 在Java中,当我这样做

Foo bar = new Foo();

bar will be a variable containing the address of the newly constructed object. bar将是一个包含新构造对象的地址的变量。 My professor call it a reference variable 我的教授称它为参考变量

The previous line is equivalent to this in C++: 上一行在C ++中与此相当:

Foo *bar = new Foo();

here, bar is a pointer to the object 这里,bar是指向对象的指针

So, is it true that a reference variable in Java is basically a pointer? 那么,Java中的引用变量基本上是指针吗?

Also, when I do this in C++: 另外,当我在C ++中这样做时:

Foo bar;

is bar also a pointer? 吧也是一个指针? If not, then does that mean there is a difference in memory structure between 如果没有,那么这是否意味着内存结构之间存在差异

Foo bar;

and

Foo *bar = new Foo();

?

Pointers in C++ behave similarly to references in Java. C ++中的指针与Java中的引用类似。

However, Foo bar; 但是, Foo bar; is very different from Foo *bar = new Foo(); Foo *bar = new Foo();非常不同Foo *bar = new Foo(); . The first creates an instance of Foo with automatic duration. 第一个创建具有自动持续时间的Foo实例。 It will be destroyed at the end of the block in which it is created. 它将在创建它的块的末尾被销毁。 The second creates a dynamically allocated instance of Foo. 第二个创建动态分配的Foo实例。 It will stick around until it is explicitly deleted. 它将一直存在,直到它被明确删除。

The first can't be used if you want the object to exist after the function returns. 如果希望在函数返回后存在对象,则不能使用第一个。 If you use a pointer or a reference to it after the function returns, bad things will happen. 如果在函数返回后使用指针或对它的引用,则会发生错误。 In that situation, you have to use the second form. 在这种情况下,您必须使用第二种形式。

The advantage of the first form is that you don't have to worry about memory management. 第一种形式的优点是您不必担心内存管理。 It will be automatically destroyed at the end of its scope. 它将在其范围的最后自动销毁。 With the second form, you have to manually delete it, or manage it with a smart pointer of some kind. 使用第二种形式,您必须手动删除它,或使用某种智能指针管理它。

So, is it true that a reference variable in Java is basically a pointer? 那么,Java中的引用变量基本上是指针吗?

--> In short YES! - >总之是的! But as c++ allow you to manipulate pointers, java does not. 但是由于c ++允许你操作指针,java则不然。 (Similar would be good word to differentiate) (类似的是区分的好词)

Foo bar; // bar is allocated on stack in c++

--> In java this is just declaration of bar of type Foo . - >在java中,这只是Foo类型bar的声明。

In c++ objects can be saved on stack as well as on heap while in Java objects are stored only on heap while object references pointing to objects are on stack. c++对象可以保存在stack中以及heapJava对象只存储only on heap而指向对象的对象引用位于堆栈上。

Foo *bar = new Foo(); Foo * bar = new Foo(); // C++ : *bar is on stack while memory allocated is on heap // C ++:* bar在堆栈上,而分配的内存在堆上

Foo bar = new Foo(); Foo bar = new Foo(); // Java : bar on stack and object created using new is on heap // Java:堆栈上的栏和使用new创建的对象在堆上

If you wonder whether the term "pointer" is appropriate for each language, given each language's specification, then yes . 如果你想知道“指针”一词是否适合每种语言,给定每种语言的规范,那么是的

In the Java language specification , Java references are called "pointers": Java语言规范中 ,Java引用称为“指针”:

An object is a class instance or an array . 对象类实例数组

The reference values (often just references ) are pointers to these objects, and a special null reference, which refers to no object. 引用值(通常只是引用 )是指向这些对象的指针,以及一个特殊的空引用,它指的是没有对象。

In the C++ language specification (which is an international ISO standard, the C++ standard ), C++ pointers are called "pointers". 在C ++语言规范(这是一个国际ISO标准, C ++标准 )中,C ++指针被称为“指针”。

Also, when I do this in C++: 另外,当我在C ++中这样做时:

Foo bar; Foo酒吧; is bar also a pointer? 吧也是一个指针? If not, then does that mean there is a difference in memory structure between 如果没有,那么这是否意味着内存结构之间存在差异

Foo bar; Foo酒吧;

It isn't a pointer. 它不是指针。 It is just an object created on the stack. 它只是在堆栈上创建的对象。

In Java all objects are references or pointers, or returns an address using new The difference between C++ pointers and Java "pointers" is that you can't have pointer arithmetic in Java. 在Java中,所有对象都是引用或指针,或者使用new返回地址.C ++指针和Java“指针”之间的区别在于,您不能在Java中使用指针算法。

And last time I read The Java Programming Language, it says references are pointers. 上次我读了Java编程语言,它说引用指针。

Well, if you want to be picky they are actually not exactly like pointers: 好吧,如果你想挑剔他们实际上并不完全像指针:

  1. Pointer arithmetic is not allowed with Java pointers. Java指针不允许指针运算。 You cannot increment, decrement, multiply, etc pointers to move what they point to in memory. 你不能递增,递减,乘法等指针来移动它们在内存中指向的东西。
  2. Not Strongly typed. 没有强烈打字。 In C, you can cast a pointer from one type to pretty much any type wth no consequence (as long as you are careful). 在C中,您可以将指针从一种类型转换为几乎任何类型的指针(只要您小心)。 In Java, you will get an exception if you try to cast between incompatible types. 在Java中,如果尝试在不兼容的类型之间进行转换,则会出现异常。

Otherwise they behave pretty much the same way as pointers, and as long as you do not need any of the above features of C-style pointers, you will probably not notice a difference. 否则它们的行为与指针几乎相同,只要您不需要C风格指针的任何上述功能,您可能就不会注意到差异。

A pointer is a value that represents a memory address. 指针是表示内存地址的值。
A reference is a value that refers to an object. 引用是引用对象的值。
Pointers are one way to realize references. 指针是实现参考的一种方式。 In fact, there are several ways to use pointers to realize references, because most objects take up more than one address, so you have to define to which address the reference points. 事实上,有几种方法可以使用指针来实现引用,因为大多数对象占用多个地址,因此您必须定义引用指向的地址。

A few differences: 一些差异:

  • You can do math with pointers, because adresses are integer values. 您可以使用指针进行数学运算,因为地址是整数值。 A reference value has no inherent meaning, it just represents an object. 参考值没有固有的含义,它只代表一个对象。
  • A pointer can point to any value, eg., a field in an object; 指针可以指向任何值,例如,对象中的字段; a reference always represents the entire object. 引用始终表示整个对象。

It would be more accurate to compare them to Pascal pointers. 将它们与Pascal指针进行比较会更准确。 They have many of the same characteristics: strongly typed, no arithmetic, can be null. 它们具有许多相同的特征:强类型,无算术,可以为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM