java对象的内存管理_java中对象的内存管理

我正在读这本关于数据结构的书,它涵盖了Java中的内存管理和孤立对象.教科书说明如下:

For example, consider the three assignment statements in the figure at

left. After the third assignment statement, not only do a and b refer

to the same Date object (1/1/2011), but also there is no longer a

reference to the Date object that was created and used to initialize

b. The only reference to that object was in the variable b, and this

reference was overwritten by the assignment, so there is no way to

refer to the object again. Such an object is said to be orphaned.

码:

Date a=new Date(12, 31, 1999);

Date b=new Date(1, 1, 2011);

b=a;

那句话是真的吗?不应该引用a(对象的内存位置Date(12,31,1999)是b的引用吗?这看起来像一个巨大的错误但是甚至有一张图片显示了12,31,1999的内存块是孤儿.

解决方法:

在java中,您总是将右侧的内容指定给左侧的引用.

所以你的陈述说出这样的话:

>将新的Date对象Date(12,31,1999)分配给变量a

>将新的Date对象Date(91,1,2011)分配给变量b

>将变量a的引用分配给变量b.

如果我遵循这些步骤,它看起来像这样:

> a – >日期(1999年12月31日)

> a – >日期(1999年12月31日)

b – >日期(2011年1月1日)

> a – >日期(1999年12月31日)

b – >日期(1999年12月31日)

请注意,在此作业完成后,日期(2011年1月1日)的原始对象不再被引用,因为您无法从应用程序中获取它.它的原始引用变量b被覆盖,现在从a和b引用对象Date(12,31,1999).日期(91,1,2011)是孤儿,准备垃圾收集.

想象一下,好像你拿着一把剑和一把斧头.首先你拿起一把剑.然后你拿起斧头.之后,你放下剑,用双手拖动你手中的斧头.在那之后你没有握住你丢下的剑(它丢失了).

编辑:如果你告诉作者这本书他会感激,这是一个错误.

标签:java,memory-management,object,memory,reference

来源: https://codeday.me/bug/20190713/1446590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值