Java Examples
Java Examples
memu home questions

Why can't AtomicBoolean be a replacement for Boolean?

Boolean is the wrapper class around the primitive boolean. It may be automatically created from a boolean by the compiler (boxing conversion) or converted to a boolean (unboxing conversion). This is not the case for AtomicBoolean where it is a separate class designed for concurrency purposes.

Hence the two classes have different semantics at the language level:

Source: (

import java.util.concurrent.atomic.AtomicBoolean;
public class Example {
    public static void main(String[] args) {
        Boolean b = new Boolean(true);
        AtomicBoolean ab = new AtomicBoolean(true);
        // automatic unboxing of Boolean variable
        System.out.println(true == b);  
        // uncommenting the following will cause a compiler error
        //System.out.println(true == ab);  

Questions answered by this page:

Can AtomicBoolean be a replacement for Boolean?