The true face of NULL in Java

Source: Internet
Author: User

Let's start with the following statement:
String x = null;


1. What exactly did this statement do?

Recall what a variable is and what is a value. A common metaphor is that a variable is similar to a box, and then you can use the box to put something in it, and you can store a value with a variable. When declaring a variable, we need to specify its type.

There are two main types in Java: Basic types and reference types. The base type stores the value, and the reference type stores the reference. In this example, the initialization statement declares a variable "x", and "X" stores the string reference, which is null here.

The following is a visual explanation of the concept:

If x= "ABC", it looks like this:

2The true face of NULL in memoryWhat exactly is null in memory, or what is the value of NULL in Java?First, NULL is not a legitimate object instance, so there is no memory allocated for it. It is simply a value that detects whether a corresponding reference points to an object.From the JVM specification:
The Java Virtual Machine specification does not specify a specific value for null.3.x true in memoryNow we all know what null is. We all know that a variable is a storage location and is an associated symbolic name that contains some values. Where does x reside in memory?
From the Java Virtual Runtime zone above, we know that each method has a private stack frame, and the local variable is stored in that frame.

The true face of NULL in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.