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.
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.
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
| Feature | boolean | Boolean |
| Type | Primitive | Object |
| Default Value | false | null |
| Can be null | No | Yes |
| Uses | Basic operations and simple flags | Used 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.
Related reading
- Delete directories recursively in Java
- Delete files, directories and buckets in amazon s3 java
- Dependency 'org.springframework.bootspring-boot-starter-web2.3.0.RELEASE' not found
- Deploying Java webapp to Tomcat 8 running in Docker container
- Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0
- Deprecated Java HttpClient - How hard can it be?
- Deserialization of JavaScript array to Java LinkedHashSet using Jackson and Spring doesn't remove duplicates
- Deserialize a json array to objects using Jackson and WebClient
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.