Java keyword NULL Usage summary

Source: Internet
Author: User

Use of 1.null

In Java, NULL is a keyword used to identify an indeterminate object. Therefore, you can assign null to a reference-type variable, but you cannot assign null to a primitive type variable. For example, we define a variable when we do this: xxxx xxxx = NULL, when the need to assign the value of xxx = new XXXX (); We know that when we define a variable, Java assigns it an initialized value, a generic primitive type int, a byte , short, long is automatically assigned to 0, float with a decimal point, double automatically assigns a value of 0.0,boolean to False, and the reference type variable is automatically assigned to NULL. It is important to note that NULL cannot be assigned to the original type (byte, short, int, long, char, float, double, Boolean) variables.

2.null and Object relationships

First look at the segment code and run the results

Package Com.test;public class Nulltest {/** * @param args */public static void main (string[] args) {//TODO auto-generated Method stub   if (Null instanceof object) {            System.out.println ("Null is Object Type");        } else {            SYSTEM.OUT.PRINTLN ("Null is not object type");}}}        

The result is no doubt: null is not an object type

3. Accessing static member variables and methods

First look at the code, you can guess the specific execution results,

Package Com.test;public class Nulltest {public static String str = "nullstring test";/** * @param args */public static void M Ain (string[] args) {System.out.println ("************"); System.out.println (((nulltest) null). str+ "  from static variables"); System.out.println ("************");  ((nulltest) null). PRINTSTR (); System.out.println ("************");} public static void Printstr () {System.out.println (str+ "   from static Function");}}

The execution results are:

NullString test  from static variable ************ nullstring test   

Without errors, this code can run correctly and get the results we expect; simple parsing: Because null values can be cast to any Java class type, (String) NULL is also legal. However, the null cast is an invalid object, its return value is NULL, and the call to the static method is bound to the class name and is not accessed by the object, so it is output correctly. Conversely, there is no static adornment that can only be accessed with an object, and a null call to the object will definitely make the null pointer wrong.

Other functions of 4.null

We can use NULL if we want to quickly release the memory resources of the object instead of expecting the GC and scanning it at the appropriate time. Let a non-null reference-type variable point to null. This object will no longer be applied by any object, waiting for the JVM garbage collection mechanism to be recycled. In other words, assigning null to a non-primitive variable is equivalent to releasing the object previously referenced by the variable.

Java keyword NULL Usage summary

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.