As we know null is an important concept in every language not only in Java but here we will study various factors regarding null.
我們知道null在每種語言中都是重要的概念,不僅在Java中,在這里我們還將研究有關null的各種因素。
null is a very critical factor that means we need to focus when we work with null.
null是一個非常關鍵的因素,這意味著我們在處理null時需要重點關注。
null is a keyword in Java and it is related to NullPointerException and NullPointerException is a package in java.lang package like this java.lang.NullPointerException.
null是Java中的關鍵字,它與NullPointerException相關,而NullPointerException是java.lang包中的一個包,例如java.lang.NullPointerException 。
We will see NullPointerException throw if we perform operations with or without null in Java.
如果在Java中執行帶或不帶null的操作,我們將看到NullPointerException拋出。
In a general way we will discuss a few cases and the cases are given below...
通常,我們將討論一些情況,下面給出了這些情況。
Case 1: We know that null is cases sensitive
情況1:我們知道null是區分大小寫的
Here, we will see why null is case sensitive in Java and null is a keyword in Java that's why null is case sensitive because all the keywords in java are case sensitive.
在這里,我們將了解為什么null在Java中是區分大小寫的,而null是 Java中的關鍵字 ,這就是為什么null是區分大小寫的原因,因為java中的所有關鍵字都區分大小寫。
Note:
注意:
Case sensitive means the word written in small letters and Capital letters has different meanings for example: null, NULL(Both are different).
區分大小寫意味著用小寫字母和大寫字母寫的單詞具有不同的含義,例如:null,NULL(兩者都不同)。
In java (null) is valid but if we write (NULL, 0, Null), etc these word is invalid and there is no sense).
在Java中(null)是有效的,但是如果我們寫(NULL,0,Null)等,則這些單詞無效,沒有任何意義。
Example:
例:
class NullCaseSensitive{
public static void main(String[] args) throws Exception{
/*We are assigning null in str1 and it will execute without any error*/
String str1 = null;
System.out.println("The value of str1 is "+str1);
/* We are assigning Null in str2 and NULL in str3
and it will give compile time error because
Null and NULL is invalid in java
*/
/*
String str2 = Null;
System.out.println("The value of str2 is "+str2);
String str3 = NULL;
System.out.println("The value of str3 is "+str3);
*/
}
}
Output
輸出量
E:\Programs>javac NullCaseSensitive.java
E:\Programs>java NullCaseSensitive
The value of str1 is null
Case 2: We know that Reference variable hold null by default
情況2:我們知道Reference變量默認為null
In Java, the Integer reference variable holds null value by default at the time of object instantiation or in other words if we don't assign any value from our end at the time of object instantiation.
在Java中,默認情況下,Integer參考變量在對象實例化時保留null值,換句話說,如果我們在對象實例化時未從結尾分配任何值,則默認為null。
In Java, String reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.
在Java中,如果我們從結束起就沒有在對象實例化時分配任何其他值,則對象實例化時String引用默認情況下為null。
In Java, Object reference hold null by default at the time of object instantiation if we don't assign any other value at the time of object instantiation from our end.
在Java中,如果在對象實例化開始時不分配任何其他值,則對象實例化時對象引用默認為null。
Example:
例:
class ReferenceVariable {
// Declaring Reference Variable
String str;
Object obj;
Integer in ;
}
class Main {
public static void main(String[] args) throws Exception {
ReferenceVariable rv = new ReferenceVariable();
System.out.println("The default value of the Object reference is " + rv.obj);
System.out.println("The default value of the String reference is " + rv.str);
System.out.println("The default value of the Integer reference is " + rv.in);
}
}
Output
輸出量
The default value of the Object reference is null
The default value of the String reference is null
The default value of the Integer reference is null
Case 3: If we assign null to primitive data type then we will get a compile-time error
情況3:如果將null賦給原始數據類型,則將獲得編譯時錯誤
Example:
例:
class AssignNullToPrimitive {
public static void main(String[] args) {
char ch = null;
int i = null;
/* We will get error here because we
can'’'t null to primitive datatype*/
System.out.println("The value of the char is " + ch);
System.out.println("The value of the int is " + i);
}
}
Output
輸出量
E:\Programs>javac AssignNullToPrimitive.java
AssignNullToPrimitive.java:5: error: incompatible types
char ch = null;
^
required: char
found: <null>
AssignNullToPrimitive.java:6: error: incompatible types
int i = null;
^
required: int
found: <null>
2 errors
Case 4: If we check whether an object is an instance of a class, interface, etc. it returns true if an object is not an instance of null (i.e the value of the expression is not null)else return false
情況4:如果我們檢查對象是否是類,接口等的實例,則如果對象不是null的實例(即表達式的值不為null),則返回true,否則返回false
Example:
例:
class CheckObjectInstanceOf {
public static void main(String[] args) throws Exception {
String str = null;
Double d = null;
Float f = 10.0f;
System.out.println("Is str is an instanceof String " + (str instanceof String));
System.out.println("Is f is an instanceof Float " + (f instanceof Float));
System.out.println("Is d is an instanceof Double " + (d instanceof Double));
}
}
Output
輸出量
Is str is an instanceof String false
Is f is an instanceof Float true
Is d is an instanceof Double false
翻譯自: https://www.includehelp.com/java/what-is-null-in-java.aspx