Array of booleans

Paul Sandoz paul.sandoz at oracle.com
Thu Mar 18 16:39:28 UTC 2021


Hmm… if I recall for VarHandle we originally only supported the full set of operations for 32 and 64-bit sized primitive types. We then expanded to all integral types, leveraging hardware if available otherwise CAS loops etc.

An atomic boolean can be supported via an int (see j.u.c.a,AtomicBoolean). I don’t recall thinking much about boolean[] arrays at the time.

In addition it’s likely we left it alone since boolean is not an integral type, with an unspecified layout, or at least out of all the primitive types could be represented differently on different VMs or potentially have more varied non-uniform layouts perhaps (for fields and arrays).

--

There is a connected case, not related to directly VarHandles though.

The Vector API already supports loading/storing a VectorMask<E> from/to a boolean[] array. 

We have recently considered adding loading/storing a Vector<Byte> from/to a boolean[] array.

For these cases though it is more contained than exposing general boolean access via a VarHandle and MemorySegment.

Paul. 

> On Mar 18, 2021, at 8:04 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> 
> Hi everybody,
> It seems it's not possible to create a VarHandle on array of booleans
>  static final VarHandle BOOLEAN_HANDLE = ofSequence(ofValueBits(8, LITTLE_ENDIAN))
>        .varHandle(boolean.class, sequenceElement());
> 
> Is there a reason for that ?
> 
> From the JVM POV, a boolean is stores on 8 bits and in the bytecode baload/bastore can load/store from either an array of booleans or an array of bytes.
> 
> regards,
> Rémi



More information about the panama-dev mailing list