Java
Boolean
Default Values
Programming
Java Data Types

Default value of boolean and Boolean in Java

Codemia Knowledge Hub ·

A boolean field defaults to false, and a Boolean field defaults to null. Those defaults apply only to fields (class-level variables). Local variables of either type get no default value, and the compiler rejects any read before they are assigned.

The primitive boolean

boolean is a primitive type that holds only true or false, and is typically used in conditions and loops. A boolean field that is not initialized is automatically false. A boolean declared inside a method must be initialized before use, otherwise compilation fails.

java
1public class BooleanExample {
2    private boolean classLevelBoolean; // This gets a default value of false
3
4    public void demonstrateBoolean() {
5        boolean methodLevelBoolean; // Must be initialized before use
6
7        // Initialization of the local boolean variable
8        methodLevelBoolean = true;
9
10        if (methodLevelBoolean) {
11            System.out.println("Method level boolean is true!");
12        }
13
14        // Using class level boolean which is false by default
15        if (classLevelBoolean) {
16            System.out.println("This won't print!");
17        } else {
18            System.out.println("Class level boolean is false by default!");
19        }
20    }
21}

The wrapper Boolean

Boolean is the wrapper class in java.lang that boxes a boolean in an object. It can hold true or false, and also null, which the primitive cannot.

An uninitialized Boolean field is null, so unboxing it in a condition such as if (flag) throws a NullPointerException. Collections and generics need Boolean, because Java collections cannot hold primitives.

java
1public class BooleanWrapperExample {
2    private Boolean classLevelBoolean; // This gets a default value of null
3
4    public void demonstrateBooleanObject() {
5        Boolean localBoolean = null; // Locals get no default, so assign null explicitly
6
7        // Intentionally triggering a NullPointerException
8        try {
9            if (localBoolean) {
10                System.out.println("This will not execute!");
11            }
12        } catch (NullPointerException e) {
13            System.out.println("localBoolean throws NullPointerException because it is null");
14        }
15
16        // Working with explicitly initialized local Boolean
17        localBoolean = Boolean.FALSE;
18        if (!localBoolean) {
19            System.out.println("Local Boolean is false!");
20        }
21
22        // Null check for class level Boolean
23        if (classLevelBoolean == null || !classLevelBoolean) {
24            System.out.println("Class level Boolean is null or false!");
25        }
26    }
27}

A local Boolean is not null by default. Like any local variable it starts unassigned, which is why the example sets it to null explicitly. Without that = null, the first if (localBoolean) is a compile error ("variable localBoolean might not have been initialized") rather than a runtime NullPointerException.

boolean vs Boolean at a glance

FeaturebooleanBoolean
TypePrimitiveObject
Default Valuefalsenull
Can be nullNoYes
UsesBasic operations and simple flagsUsed in collections, generics, and where nullability is needed

Use boolean for simple flags and control flow, and Boolean when you need collections, generics or a "not set" state, with a null check before unboxing.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms