what happens in memory when I, say, do the following:
in C: char *c=NULL;
in java: MyClass mc=null;
what happens in memory, how is this null represented in memory in both of these languages? Thanks
what happens in memory when I, say, do the following: in C: char *c=NULL;
Share
In C you are basically setting the pointer to zero value. In fact
is equivalent to
Zero pointer cannot be dereferenced since there is no memory mapping for this address. On platforms with virtual memory, an attempt to do so triggers a page fault transferring control to the operating system which then usually handles the situation by killing the offending process (in UNIX your process receives Segmentation Violation signal
SIGSEGV).In Java, all non-primitive type variables are references.
nullin Java is a literal denoting the only value of a special unnamed type. This type can be cast to any reference type allowing you to putnullinto any reference variable. An attempt to use such reference to access an object will throw an unchecked exception calledNullPointerException. See JLS 3.10.7 and JLS 4.1 for details.