Redian新闻
>
please help me to solve this question!
avatar
please help me to solve this question!# Java - 爪哇娇娃
b*n
1
The following code creates one Point object and one Rectangle object. How many
references to those objects exist after the code executes? Is either object
eligible for garbage collection?
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
...
My answer: only one object exists after the code executes. point object is
eligible for garbage collection.
Is my answer right?
avatar
w*r
2
nah.. before the GC, theere were 2 objcts, one is the instance of Point
another is the instance of Rect, however, the pint references to null, if
there were not other references to the Point object, that object is eligible
for GC.

many

【在 b*******n 的大作中提到】
: The following code creates one Point object and one Rectangle object. How many
: references to those objects exist after the code executes? Is either object
: eligible for garbage collection?
: ...
: Point point = new Point(2,4);
: Rectangle rectangle = new Rectangle(point, 20, 20);
: point = null;
: ...
: My answer: only one object exists after the code executes. point object is
: eligible for garbage collection.

avatar
B*N
3
after this code get execute, how many objects eligible for garbage collection
is depending on context.
Let's say if no other object hold reference "point" and "rectangle", then both
eligible for GC. If something hold it like array or vector, then it will not
be GC. IN either case "point = null" makes no difference. So you can remove
it. It's different as c++ "delete".
example:
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
mVector.add(point)
.

【在 b*******n 的大作中提到】
: The following code creates one Point object and one Rectangle object. How many
: references to those objects exist after the code executes? Is either object
: eligible for garbage collection?
: ...
: Point point = new Point(2,4);
: Rectangle rectangle = new Rectangle(point, 20, 20);
: point = null;
: ...
: My answer: only one object exists after the code executes. point object is
: eligible for garbage collection.

avatar
B*N
4
sorry! wrong logic.
I really means this:
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
mVector.add(point):
point = null;
....
in this case point = null dosn't make sure point is GC.
in the original post the "point" is GCed, because the "rectangel" is not
really hold the "point" reference, it just get it's x and y value. It's
different as you put it in an array or vector. Whether the "rectangle" GC
depends on Context.

collection
both
object

【在 B******N 的大作中提到】
: after this code get execute, how many objects eligible for garbage collection
: is depending on context.
: Let's say if no other object hold reference "point" and "rectangle", then both
: eligible for GC. If something hold it like array or vector, then it will not
: be GC. IN either case "point = null" makes no difference. So you can remove
: it. It's different as c++ "delete".
: example:
: ...
: Point point = new Point(2,4);
: Rectangle rectangle = new Rectangle(point, 20, 20);

avatar
B*N
5
my brain messed up, damn.
I mean GC is eligible for Garbage Collected. Not real time GC. sorry again.

not
remove
this
How

【在 B******N 的大作中提到】
: sorry! wrong logic.
: I really means this:
: ...
: Point point = new Point(2,4);
: Rectangle rectangle = new Rectangle(point, 20, 20);
: mVector.add(point):
: point = null;
: ....
: in this case point = null dosn't make sure point is GC.
: in the original post the "point" is GCed, because the "rectangel" is not

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。