You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Reference Stability is Sometimes Necessary

When a boxed value is being used as a simple reference, sometimes reference identity must be preserved.
This means there must be defined scopes where reboxing is not allowed.

CAS of value into Object reference field
AtomicReference<Object> ref = new AtomicReference<Object>();
Object c1 = Complex.valueOf(1, 0);
Object c2 = Complex.valueOf(0, 1);
ref.set(c1);
boolean ok;
ok = ref.compareAndSet(c1, c2);
assert(ok);

The assertion can fail if reboxing of c1 is allowed in this code.

  • No labels