问题
This is in continuation to my previous question and accroding to answers of this question Declaration of wrapper classes
Java wraps primitive data type to wrapper classes then why
char c = null; // invalid
int i = null; // invalid
is not allowed but
Character cObj = null; // valid
Integer iObj = null; // valid
is allowed.
回答1:
Because primitives represent value and Object variables represent references (something like pointers) to complex data objects. There is no null value general, it is a special keyword that "references to nothing" or empty reference - this is highly unprofessional answer, but I guess it will be most appropriate.
Besides, what could be in your opinion, numerical value of null? 0? -1? But, those are valid integers so what else?
I strongly suggest that you start familiarizing yourself with the following complex java tutorial. Every aspect that you have been asking about is explained there and supported with examples.
回答2:
null means "lack of an object". References can lack an object, primitives can't.
回答3:
Java primitive type variables are store-by-value instead of store-by-reference variables. Where as the wrapper classes are objects basically like any other Java object except that all they do is wrap a primitive type.
回答4:
The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.
As per jls-4.3.1, it is pointless to take about the null reference without the existence of an object.
回答5:
Along with all above answer i would like to add this point too.
For primitive types,we have fixed memory size i.e for int we have 4 bytes and char we have 2 bytes. And null is used only for objects because there memory size is not fixed.
So by default we have,
int a=0;
and not
int a=null;
Same with other primitive types and hence null is only used for objects and not for primitive types.
回答6:
Objects like Character and Integer are pointers: the actual number stored in the bytes that are that variable's value represents an address in memory for the rest of the JVM's memory. So, it is possible and meaningful to set that number to an address that goes nowhere, which is what null is.
A primitive like int or char, however, has a number that is interpreted as a number (integer, or ASCII code), and there's no way to make it "not a number" because all that the memory can possibly store is numbers.
回答7:
Referring to unboxing/autoboxing, you have to imagine them like are two ways that the compiler adopt to save you from going mad with continuos "casting" from primitive to object and vice-versa, but they are not flawless.
What happens if your Integer wrapper is null and you do a division? Not a division by 0 but a Null pointer exception, because java cannot unbox a not referenced object!
So it's safe and logical to keep different init rules for primitives and objects.
来源:https://stackoverflow.com/questions/19511616/why-java-does-not-allow-null-while-declaring-primitive-data-types