I am trying to understand how null
works in Java.
If we assign null
to any object, what happens actually behind the scene? Does it assign a memory location address pointing to a null
"object" or something else?
I've tried the following program and I've come to understand that all null
s point to same location.
But can anybody tell me how Java throws NullPointerException
and how null
works in Java?
class Animal{
}
class Dog{
}
public class testItClass {
public static void main(String args[]){
Animal animal=null;
Dog dog=null;
if(((Object)dog) == ((Object)animal))
System.out.println("Equal");
}
}
Output
Equal.
The method isNull is a static method of the Objects class in java that checks whether the input object reference supplied to it is null or not. If the passed object is null , then the method returns true . If the passed object is non-null , then the method returns false .
Below are some important points about null in java that every Java programmer should know: 1. null is Case sensitive: null is literal in Java and because keywords are case-sensitive in java, we can't write NULL or 0 as in C language.
For example, if you want a variable but don't want it to actually have an object, you can just declare the variable without initializing it: Name myName; In this case myName will be null (or "unassigned"? depends on the context), but will be of type Name and can be used as such later (once it's assigned a value).
if we assign null to any object what it actually is it some memory location in heap OR anything else.
One should distinguish reference
and object
. You can assign null
to a reference.
Objects are normally created in heap using new
operator. It returns you a reference to an object.
A a = new A();
object with type A
is created in heap. You are given back reference a
. If now you assign
a = null;
the object itself still reside in heap, but you would not be able to access it using reference a
.
Note that object might be garbage collected later.
UPD:
I have created this class to see byte code of it (first time to me):
public class NullTest {
public static void main (String[] args) {
Object o = new Object();
o = null;
o.notifyAll();
}
}
And it produces:
C:\Users\Nikolay\workspace\TestNull\bin>javap -c NullTest.class
Compiled from "NullTest.java"
public class NullTest {
public NullTest();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #3 // class java/lang/Object
3: dup
4: invokespecial #8 // Method java/lang/Object."<init>":()V
7: astore_1
8: aconst_null
9: astore_1
10: aload_1
11: invokevirtual #16 // Method java/lang/Object.notifyAll:()V
14: return
}
You can see that set null to a reference results:
8: aconst_null
9: astore_1
List of byte code instructions
Basically it puts value of null to the top of stack and then saves to the reference. But this mechanism and reference implementation is internal to JVM.
How is reference to java object is implemented?
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