Reputation: 7267
Can anyone explain why this code results in the below output?
@Test
public void testBooleanArray() {
Boolean[] ab = new Boolean[]{a, b};
a = new Boolean(true);
b = new Boolean(false);
for(Boolean x : ab) {
System.out.println(x);
}
}
Result:
null
null
Should the array ab not holds pointers to object a and object b, and therefore output:
true
false
Upvotes: 13
Views: 1282
Reputation: 2287
I think it's helpful to visualize the array elements as pointers.
We first create two pointers, a
and b
, both pointing to null.
Boolean a = null, b = null;
Next, we create two more pointers, ab[0]
and ab[1]
, and point them at the same place as a
and b
. That is, null
.
Boolean[] ab = new Boolean[]{a, b};
Then, we create new Boolean true
and false
objects (with the new Boolean(true)
and new Boolean(false)
parts of the statements).
Finally, we let a
and b
point to them.
a = new Boolean(true);
b = new Boolean(false);
When you look at it this way, I think it's more clear why changing a
and b
has no effect on the array.
Upvotes: 4
Reputation: 14269
Your code unrolled:
Boolean a = null;
Boolean b = null;
Boolean[] ab = new Boolean[2];
ab[0] = a;
ab[1] = b;
a = new Boolean(true);
b = new Boolean(false);
The moment the content of the variables named a and b was copied to the array, it was set to null. There is an important difference to copy by value and copy by reference.
As a side-note: it is recommended to use Boolean.TRUE instead or at least Boolean.valueOf(true), to avoid unnecessary object creation. There aren't that many options for a boolean value and Boolean is immutable.
Upvotes: 6
Reputation: 19607
... Which is absolutely normal. You are initializing values but, sequentially speaking, a
and b
are still null
before the process gets round to assigning the variables. It's not the variables that are being placed but their values or references as elements in an array.
@Test
public void testBooleanArray() {
/* We have a Boolean array, Boolean being able to hold a true, false but
also a null as an object */
Boolean[] ab = new Boolean[]{a, b}; /* We initialize the array with the variables
here but ... */
a = new Boolean(true); // ... the instances are only set at this stage
b = new Boolean(false); /* Plus, even if Boolean is immutable, the use of new
will actually create new objects */
for(Boolean x : ab) {
System.out.println(x);
}
}
Upvotes: 3
Reputation: 7812
a = new Boolean(true);
b = new Boolean(false);
This does not change the objects that a and b were pointing to(the elements in the array). It points them to new
objects.
It is not modfying the array
To illustrate:
Boolean a = new Boolean(true);
Boolean b = new Boolean(false);
Boolean c = a;
Boolean d = b;
a = new Boolean(false);
b = new Boolean(true);
c and d will still be true/false respectively. This is the same thing that is happening with the array, except your array reference isn't named the same way.
Upvotes: 21
Reputation: 9038
You have to initialize your booleans before assigning them.
Boolean[] ab = new Boolean[]{a, b};
a = new Boolean(true);
b = new Boolean(false);
to
a = new Boolean(true);
b = new Boolean(false);
Boolean[] ab = new Boolean[]{a, b};
This is before with Objects, you copy the reference to the object, and with new statement, you create a new object, the first a,b were null when assigning.
Upvotes: 8