Reputation: 924
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.
Upvotes: 10
Views: 1358
Reputation: 10423
On the bytecode level there is an instruction to push a "null" to the local stack (and then assign it to a variable slot or field).
Most Java Virtual Machines are actually using real (machine) pointers assigned to null. So if they are used they internally trap the signal (SIGSEGV) and convert it into a NPE. You can actually test this by sending a "kill -SEGV" to a Java PID, it will generate random NullPointerExceptions most of the time.
Upvotes: 0
Reputation: 575
When an object is set to null
then basically it has no memory reference in the JVM. Also, ==
comparison between two null objects would also yield a true value.
Upvotes: 0
Reputation: 11
Null in java is designed based on NULL Object pattern.
NULL Object means Non-existent Object. If we try to access any resource from this Non Existent Object we should not get any thing, this logically make sense. But practically the programming languages like Java will create instance of NullPointerException class and populate the required information into the Object and throw at calling statement.
Upvotes: 1
Reputation: 9579
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?
Upvotes: 4
Reputation: 21728
There is only one null
in Java. It is the same in all cases it arises, regardless to the variable and even regardless of the class. Compiler will not allow to compare directly but you can cast:
String x = null;
ArrayList a = null;
if ((Object) x == (Object) a) {
System.out.println("Do not have onther nulls, only ME");
}
Upvotes: 0
Reputation: 31952
It outputs true because null == null
Null just means nothing.
Animal animal=null;
Defines a reference to an Animal
object, but is not yet assigned to one, so it points to nothing.
If you are experienced with C/C++, it is like setting a pointer to NULL
or nullptr
.
Upvotes: 0
Reputation: 500157
The following declares a reference to an object of class Animal
(or a subclass thereof), and initializes it to null
:
Animal animal = null;
Here, the reference itself takes up some space. Since there is no associated object, there's no further memory cost.
If you try to use a null
reference by accessing the object:
Animal cat = null;
if (cat.equals(dog)) { ... } // throws NPE
you'll get a NullPointerException
.
It is, however, OK to operate on the null
reference itself, as long as you don't try to dereference it:
Animal cat = null;
Animal dog = null
if (cat == dog) { ... } // works fine
Upvotes: 1
Reputation: 153
Null is in fact a memory reference to nothing (hence null). All object references in code are pointers to locations in memory where the physical data is kept. If an object is null then it has no reference, or points at nothing in memory. This is why null cannot be used for anything else except checking whether it is null.
Java will throw a null exception when an attempt is made to manipulate an object that has no physical memory reference.
Upvotes: 0