Default value of 'boolean' and 'Boolean' in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, there are two types that can represent boolean values: the primitive type boolean and the object-wrapper class Boolean. Understanding the default values and the implications of using each type can help developers write cleaner, more efficient code. This article will discuss these differences, along with some practical examples.
Understanding boolean
The boolean type in Java is a primitive data type. It is used to store only one of two possible values: true or false. This is generally used in conditions and loops for control flow in a program.
The default value of a boolean in Java is false. This is crucial to know, especially when declaring a boolean variable at the class level, which will automatically be assigned this default value unless explicitly initialized. If a boolean field is declared within a method, it must be initialized before use; otherwise, the compiler will throw an error as local variables do not receive default values.
Here's an example of boolean usage in Java:
Understanding Boolean
Boolean, on the other hand, is a wrapper class in Java provided in the java.lang package. It wraps the primitive boolean type in an object. An instance of Boolean can store the same values as a primitive boolean—true or false—but it can also store a null value, which is not possible with the primitive type.
The default value of a Boolean object, when not explicitly initialized, is null. This notably differs from the boolean primitive and is vital to remember to avoid NullPointerExceptions. When working with collections or generic data types, Boolean is used because Java collections cannot handle primitives.
Here's an example to illustrate:
Comparison and Summary
Below is a table that summarizes the critical differences between boolean and Boolean in Java:
| 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 |
Conclusion
Both the boolean and Boolean types are fundamental to controlling flow in Java programming, but they serve different purposes. While boolean is efficient for control structures and flags due to its simplicity and speed, Boolean is indispensable in object-oriented and generic programming where nullability and collection compatibility matters. Understanding the distinction and default values can significantly improve the robustness and correctness of Java applications.
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

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.