No, primitive data types in Java cannot hold a null value. Primitive variables store actual values directly and must always contain a valid value of their respective type.
Key Points: • Primitive data types store actual data, not object references. • A primitive variable cannot be assigned null because null represents the absence of an object reference. • Primitive types have predefined default values when declared as instance variables. • Wrapper classes such as Integer, Double, and Boolean can hold null values because they are objects. • Attempting to assign null to a primitive variable results in a compilation error.
Example: int age = 0; // Valid Integer salary = null; // Valid
Code Example:
public class Demo {int number; // Default value is 0
public static void main(String[] args) {
// int value = null; // Compilation ErrorInteger value = null; // Valid
}
}Interview Tip: A concise interview answer is:
"No, primitive data types cannot be null because they store actual values rather than object references. If a null value is required, wrapper classes such as Integer, Long, or Boolean should be used instead."
Quick Comparison:
Primitive Type: • Stores actual value • Cannot be null • Example: int, double, boolean
Wrapper Class: • Stores object reference • Can be null • Example: Integer, Double, Boolean