Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:01:39+00:00 2026-06-18T02:01:39+00:00

I am trying to understand how null works in Java. If we assign null

  • 0

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 nulls 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:01:40+00:00Added an answer on June 18, 2026 at 2:01 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Java and was trying to understand how synchronizatioon works. So
I'm trying to understand how the Java class loader works, specifically the defineClass(String,byte[],int,int) method.
Hi so I'm trying to understand how inherteince works in PHP using object oriented
I am a student learning C++, and I am trying to understand how null-terminated
I'm trying to understand how validation works for a combo box when its ItemsSource
I have been trying to understand the use of primitives in Java and C#
I am trying to understand how the requests works. Unfortunately I was thrown at
I'm trying to understand better how data binding works in .net. I was checking
I have spent hours trying to understand why my logger class does not work.
Possible Duplicate: How does JavaScript .prototype work? Coming from Java background, I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.