Junior — Middle
Is it allowed to assign a null value to a variable of a primitive type?
sobes.tech AI
Answer from AI
In Java, a variable of a primitive type (e.g., int, boolean, double) cannot be assigned the value null because primitives are not objects and always contain a specific default value (e.g., 0 for int).
If you need to be able to assign null, you should use the corresponding wrapper classes, such as Integer, Boolean, Double. They are objects and can accept the value null.
Example:
int a = null; // Compilation error
Integer b = null; // Allowed