1

I was asked by my text to count the references to an object. Inside the given code was the following:

Honey honeypot = new Honey();

Honey [ ] ha = {honeypot, honeypot, honeypot, honeypot};

Bees b1 = new Bees();

b1.beeHA = ha;

where the constructor for Bees creates the Honey[ ] beeHA array.

Now I thought that beeHA would now hold the reference honeypot in four positions as does array ha and that these would be four separate references plus the four from the ha array making eight references. But the diagram the book gives as an answer say that the beeHA array only points to the ha array not to the object referenced by the honeypot reference variable making only four references. Am I thinking about this correctly?

If array ha were to be garbage collected (subject of the chapter) wouldn't b1.beeHA[x] still contain the honeypot reference variable?

3 Answers 3

2

The crucial distinction in the Java type system is between value semantics and reference semantics: When you say x = y;, then values are copied, and references are aliased.

Primitive types (like int) have value semantics; class-types and arrays have reference semantics. So when you say b1.beeHA = ha;, you alias the reference to the array, which is to say that now both ha and b1.beeHA reference the same array.

The garbage collector won't collect the array while there are still references to it.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually, I think even that makes it more complicated than it needs to be. Assignment always copies the value of the RHS to the LHS. It's just that if x and y are of a class type, then the value of the variable is a reference - that's all. There's no aliasing going on, in that the x and y variables aren't tied together - they just happen to have the same value, which is a reference.
@JonSkeet: I think what you're describing is the implementer's perspective. From the language perspective, there are no "reference types", so your statement cannot really be expressed within in the language itself, if that makes sense. (E.g. the type of honeypot, as specified by the Java type system, is really Honey, and not some mystaculous "assignable reference to Honey".)
If there are no "reference types" in the language, why is JLS section 4.3 entitled "Reference types and values"? And from section 4.3.1: "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." Nothing mystaculous there - it's all in the spec. It differentiates very clearly between references and objects. I can't find your use of the term "alias" in the spec though...
(And likewise from the start of section 4.1: "There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).")
@JonSkeet: Fair enough. I suppose what I meant is that the type system does not have distinct types Honey and "reference to Honey". The code suggests that honeypot is a variable of type Honey, so I find the reasoning in terms of value vs reference semantics rather more systematic; but you're right of course that the JLS does describe reference types.
|
1

ha isn't an array - it's a variable. The value of the variable is a reference to the array - as is the value of b1.beeHA. So it doesn't really make sense to talk about "array ha" being garbage collected. When you write:

b1.beeHA = ha;

that simply copies the value of ha into b1.beeHA. It's not copying the array - it's just copying the reference.

The code you've shown creates one array containing four references to one instance of Honey. There are two references to that array - one in the variable ha and one in the variable b1.beeHA.

1 Comment

this makes the most sense to me. I suppose i was thinking there were two separate array objects and not just one with two references. Thanks everyone!
0

Your example is a bit confusing without more context; I can't see scope on any of these variables.

An object won't be garbage collected if there's an object holding onto a reference to it. The honeypot reference points to the new instance of Honey; so does the ha array. The b1 instance points to the ha array.

If you draw an object graph, the b1 instance points to ha, which points to honeypot.

If b1 is garbage collected, then the whole graph is eligible.

The Honey instance won't be GC'd as long as b1 and ha are around.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.