在Java中,对象变量是否包含对象的地址? [英] In Java, does an object variable contain the address of an object?

查看:82
本文介绍了在Java中,对象变量是否包含对象的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正和老师谈话,她提到一个对象变量(她的意思是一个对象的实例)不包含对象本身,而是包含内存中的地址。

I was talking with my teacher, and she mentioned that an object variable (she means an instance of an object) did not contain the object itself, but rather the address in memory.

我听说在Java中,对象的实例确实包含对内存中对象的引用。我错了吗?引用与包含内存中的地址或其他内容相同吗?

I've heard that in Java, an instance of an object really contains a reference to an object in memory. Am I wrong? Is a reference the same thing as containing the address in memory, or something else?

推荐答案

对象变量 isn' t 与对象的实例相同。区分变量,它们的值和对象是非常重要的。

An object variable isn't the same as an instance of an object. It's really important that you distinguish between variables, their values, and objects.

例如:

String x = "hello";

变量 x 。它就像一张纸,上面写着

The variable is x. It's like a piece of paper, which can have a value written on it.

变量的是一个引用 - VM可用于获取字符串对象本身的一些数据。它不一定是一个地址 - 它只是获取对象数据的一种方式。 (有关详细信息,请阅读Eric Lippert的博客文章引用不是地址 - 这是关于C#而不是Java,但它的原理是相同的。)

The value of the variable is a reference - some data that the VM can use to get to the string object itself. It doesn't have to be an address - it's just "a way of getting to the object data". (For more on this, read Eric Lippert's blog post "References are not addresses" - that's talking about C# rather than Java, but it's the same principle.)

对象本身就是一个独立的实体。

The object itself is a separate entity.

要使用一个真实世界的例子,想象一下我的家庭地址上写着一张纸。这里有三件事:

To use a real-world example, imagine I have a piece of paper with my home address written on it. There are clearly three things here:


  • 这张纸(就像一个变量)。这张纸本身不是我的家庭住址,也不是房子。这只是可以存储价值的东西。

  • 我的家庭住址不是一张纸,也不是房子。这只是让某人 到我家的价值。

  • 我的房子既不是纸张,也不是地址本身。这是一个对象。

  • The piece of paper (like a variable). The piece of paper itself isn't my home address, nor is it a house. It's just something which can store a value.
  • My home address isn't a piece of paper, nor is it a house. It's just a value which lets someone get to my house.
  • My house is neither a piece of paper, nor an address in itself. It's an object.

当你考虑参数传递和变量赋值之类的东西时,这变得很重要。例如:

This becomes important when you consider things like parameter passing and variable assignment. For example:

House x = new House("Jon");
House y = x;

这里我们有两个变量, x y ,就像两张纸一样。我们建造一所房子,并在 x 上写下指示。然后,我们 x 上写的值复制 y 。请注意,它们仍然是完全独立的纸张 - 但它们目前具有相同的值。只有一个对象 - 我们只建了一个房子 - 但现在两张纸的方向相同。

Here we have two variables, x and y, like two pieces of paper. We build a house, and write down the directions to it on x. We then copy the value that's written on x into y. Note that they're still completely separate bits of paper - but they currently have the same value written on them. There's only a single object - we've only built one house - but now two pieces of paper have the same directions on them.

如果一个人按照纸上的指示 x 并将前门涂成红色,然后第二个人按照纸上的指示 y ,他们会找到一个带红色前门的房子。

If one person followed the directions on piece of paper x and painted the front door red, then a second person followed the directions on piece of paper y, they'd find a house with a red front door.

另一方面,如果一个人在纸上抄写指示 x ,这不会影响纸上写的指示 y

On the other hand, if one person scribbled out the directions on piece of paper x, that wouldn't affect the directions written on piece of paper y at all.

这篇关于在Java中,对象变量是否包含对象的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆