Home >Java >javaTutorial >The meaning of null in Java and things to pay attention to when using it

The meaning of null in Java and things to pay attention to when using it

黄舟
黄舟Original
2017-09-18 09:40:031337browse

The following editor will bring you a brief discussion of what null is in Java and what should be paid attention to when using it. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

1.null is neither an object nor a type. It is just a special value. You can assign it to any reference type. You can also Convert null to any type, for example:

Integer i=null;
Float f=null;
String s=null;

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

int k=null ----- -----The compiler will report an error cannot convert from null to int

2.null is a keyword, like public, static, and final. It is case-sensitive. You cannot write null as Null or NULL, otherwise the compiler will report an error

3. A wrapper class containing a null value is unboxed and generated in Java A null pointer exception will be thrown whenever the data type is used

For example:

Integer i=null;
int k=i;---------- ------------------Throws java.lang.NullPointerException

4. When traversing the collection or array, you need to add null judgment, otherwise when the collection or array contains null, an exception will be thrown

5. Use equals to determine whether the string is When equal, the constant string should be placed on the left side of equals to prevent null pointer exception

For example:

String[] arr1={"abc","123",null,"sky"};
for (String s1 : arr1) {
boolean flag=s1.equals("sky");
}

------------When fetching When the value reaches =null, a null pointer exception will be thrown. Change s1.equals("sky") to "sky".equals(s1) to avoid throwing exceptions

6. The difference between empty string and null

Type

null represents the value of an object, not a string. For example, when declaring a reference to an object, String a = null;

"" represents an empty string, which means its length is 0. For example, declaring a string String str = "" ;

Memory allocation

String str = null ; means declaring a reference to a string object, but the pointer is null, which means that it has not yet pointed to Any memory space;

String str = ""; means declaring a reference of string type, its value is ""empty string, this str reference points to the memory space of empty string;

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

The above is the detailed content of The meaning of null in Java and things to pay attention to when using it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn