Java 中的对象引用 [英] Object References In Java

查看:43
本文介绍了Java 中的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java 中的对象引用让我很困惑.当我开始研究链表时,我意识到我对这个主题知之甚少.我的问题是

Object references in java really confuse me. And when I started to study linkedlist, I realized that I know verry little about the subject. My question is

Node node = list; 

假设列表是我链表中的第一个节点,我知道这个操作意味着节点引用了列表引用的同一个对象.我的意思是,如果我对节点进行更改,因此对它所引用的对象进行更改,列表也会受到这些更改的影响.但是,我不明白的是, node.next 和 list.next 也指的是同一个对象.我的意思是,如果我像

Let's say list is the first node in my linkedlist and I know that this operation means that node refers to the same object that the list refers to. I mean, if I make changes to the node, consequently to the object that it refers to, list will be also affected by these changes. However, what I don't understand that do node.next and list.next also refer to the same object. I mean if I wrote the linkedlist like

Node node = list;
node = node.next;

这是否意味着我也在使用 list.next 更改列表?或者,如果我写 (node.next.name = "B"),我是否也更改了 list.next 元素的名称?.我知道这些问题看起来很愚蠢,但我真的对参考资料一无所知.我也研究了很多这个主题.但是到目前为止,我在网上找到的内容对我没有太大帮助.如果有人以清晰易懂的方式向我解释,我将不胜感激.

Does that mean I am also changing list with the list.next? Or if I write (node.next.name = "B"), do I also change the name of the list.next element?. I know the questions seems silly but I really don't know anything about references. And I also researched the subject a lot. But what I found online so far, they didn't help me much. If someone explains it to me in a clear and comprehensible way, I would be very appreciated.

推荐答案

你知道一个引用意味着在内存中,它们对内存中的数据有相同的引用".

You know a reference means that in the memory, they have the same "reference" to the data in memory.

所以很简单,nodelist 是同一个对象.

So it is as simple as node and list are the same object.

如果您认为数据存储在内存(RAM)中.把记忆想象成一个装满抽屉的壁橱,

If you consider that the data is stored in the memory (RAM). Consider the memory as a closet full of drawers,

如果node的数据在2号抽屉里.by Reference"表示node = list表示list也引用2号抽屉.

if the data of node are in drawer number 2. "by Reference" means that node = list means that list also refers to drawer number 2.

现在无论你在数据中做什么改变,你都会改变抽屉编号 2 的值.所以你同时改变了 nodelist

Now whatever you change in the data, you're changing the values of drawer number 2. So you're changing both node and list

这篇关于Java 中的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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