What is null in Java, and what to note in using

Source: Internet
Author: User

1. NULL is neither an object nor a type, it is only a special value that you can assign to any reference type, or you can convert null to any type, for example:

Integer I=null;
Float F=null;
String S=null;

However, you cannot assign null to basic types, such as int, float,double, etc.

int k=null----------Compiler will error cannot convert from null to int

2. null is a keyword, like public, static, final. It is case sensitive, you cannot write NULL as NULL or NULL, or the compiler will error 3. wrapper class with null value throws a null pointer exception when Java unboxing generates a basic data type For example:Integer I=null;int k=i;---------------------------thrownjava.lang.NullPointerException 4. You need to add a null judgment when iterating through a collection or array, or it throws an exception when the collection or array contains null   5. When using equals to determine whether a string is equal, place the constant string on the left side of equals to prevent null pointer exceptions For example:

String[] arr1={"abc", "123", NULL, "Sky"};
for (String s1:arr1) {
Boolean Flag=s1.equals ("Sky");
------------ When the value is =null, a null pointer exception is thrown and the S1.equals ("Sky") is changed to "Sky". Equals (S1) to avoid throwing exceptions

6. Empty string and Null difference

  Type
Null represents the value of an object, not a string. For example, declaring a reference to an object, String a = null;
"" represents an empty string, meaning that it is 0 in length. For example, declare a string str = "";

  Memory allocation
String str = NULL; Represents a reference that declares a string object, but points to null, which means that no memory space has been pointed to;
String str = ""; Represents a reference to a string type, with a value of "" an empty string, which refers to the memory space of an empty string;

In Java, variables and reference variables are present in the stack (stack), and objects (new) are stored in the heap (heap):

What is null in Java, and what to note in using

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.