If I declare some string variable like
String str = null;
str
? str
been declared?null
value inside the memory, then what exactly is the representation of null
in binary?It means that there is no value associated with name . You can also think of it as the absence of data or simply no data. Note: The actual memory value used to denote null is implementation-specific. For example the Java Virtual Machine Specification states at the end of section 2.4. “ Reference Types and Values:”
If you're in a native language (C and C++, for instance), NULL is a pointer with a zero value, and that points to the memory base address. Obviously that's not a valid address, but you "can" dereference it anyway - especially in a system without protected memory, like old MS-DOS or small ones for embedded processors.
Yes memory is allocated to null values as well.
Firstly, a Java double cannot be a Java null , and cannot be compared with null . (The double type is a primitive (non-reference) type and primitive types cannot be null .)
When you do something like:
String str = null;
The only thing allocated is a reference to a string (this is analogous to a string-pointer in languages which contain pointer-types). This reference is a 32 or 64 bit variable (depending upon your hardware architecture and Java version) which resides on the stack (probably, depending upon the exact context in which your declaration is placed).
No memory should be allocated for the null
itself, because null
is not actually a valid object instance. It is simply a placeholder that indicates that the object reference is not currently referring to an object. Probably it is simply the literal value 0
, such that assigning an object reference to null
is equivalent to setting a pointer type to NULL
in C/C++. But that last bit is conjecture on my part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With