In Java, only one object is declared that is not assigned a value, and the difference between declaring an object and assigning null

Source: Internet
Author: User

Like what:

Public Maingame Mmaingame; With public maingame mmaingame = null; the difference between the two sentences?


Compile time will be different, unassigned, directly using the compile will be an error.

Without assigning space, assigning null allocates the initial space of 0 size.


The local variable is assigned the initial value, the global variable does not need to be assigned the initial value, and the int a=null; this sentence should be an integer a=null;
    • If you declare a variable in a class that can not be assigned a value, you get the default value when you construct the object

    • If you declare a variable in a method, you must assign an initial value

As a member variable, int A; the default value is 0, but int a=null; in Java this is not true; int is the basic type;
Only the default value of the package type Integer is null so you can write int a;(0) Integer A; (null)
The object reference in Java is placed on the stack, the instance of the object is placed in the heap, and if NULL, the description is only in the stack.
The first thing to understand is that when the object in Java is passed, it is a reference (that is, the object's address), which is much more efficient than passing the whole object. The underlying type, int,double, and so on, is the value that is passed. For example, (new arraylist<string>). Add (new string ("Hello")), the JVM simply deposits the address of the new string ("Hello") into the list of lists. String str = new String ("Test"), is the opening up of memory into the object and assigns its reference to Str. Similarly, BookInfo bookinfo=null and BookInfo bookinfo=new BookInfo (): The former, is the declaration of an object (the reference), the JVM does not open up memory into an object, and the latter, after declaring a reference to an object, He was assigned the address of the newly opened object that did not store any valid values.

In Java, only one object is declared that is not assigned a value, and the difference between declaring an object and assigning null

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.