Java和null共享唯一的结合。 几乎所有Java开发人员都会遇到NullPointerException麻烦,这是有关null和Java的最臭名昭著的事实。 在本期刊中,我们将了解Java中的null。
空值已经存在了很长时间,我们都知道空值会带来更多的问题,而不是解决的问题。 因此,让我们尝试更多地了解null的概念,并确保我们正确使用它。
许多开发人员已经熟悉null,但对于不熟悉null的开发人员,他们可以学习有关null关键字的一些新旧知识。
What is null?
null是Java中一个非常重要的概念。 发明null的初衷是表示没有东西。 但是多年来,它使讨厌的NullPointerException困扰着Java开发人员。
[Read More: How to Handle Null Pointer Exception in Java
在开始之前,让我们回顾并理解什么是变量,什么是值? 变量基本上是在其中存储值的容器。 每次声明变量时,我们都需要指定类型,该类型将定义将存储在变量中的值的类型。
在Java中,主要有两种类型:基本类型和引用类型。 声明为原始类型的变量存储值,而声明为引用类型的变量存储引用。 下面给出的是变量和分配给这些变量的值的基本示例。
package in.developersjournal;
public class Main {
// Uninitialised variable of reference type.
// Hence it will store null in it.
private static Object djObject;
// Uninitialised int. It is of primitive type.
// Hence it will store 0 until initialised.
private static int firstInt;
public static void main(String[] args) {
// Initialised integer with value 10.
int secondInt = 10;
System.out.println(secondInt);
System.out.println(firstInt);
System.out.println(djObject);
}
}
上面的代码片段声明了3个变量:
- 对象djObject –引用类型int firstNumber –基本类型int secondNumber –基本类型
这里,前两个变量在声明它们时未初始化,而第三个变量用数字10初始化。由于Object是引用类型,因此它在其中存储null,而firstNumber将0存储为默认值。 上面的代码片段的输出显示在下面的图1中。
Figure 1: OUTPUT of the code snippet written above.
Properties of null
#null Keyword / Reserved Word
在Java中,null是字面值的保留字(关键字)。 它看起来像一个关键字,但实际上,它是与true和false相似的文字。 保留字null区分大小写,我们不能将null编写为Null或NULL,编译器将无法识别它们并给出错误。
Object djObject1 = NULL; // Not Ok - Compile time error
Object djObject2 = null; // Ok
#null used as a default
如果在声明时未进行初始化,则变量的每个原始类型都具有默认值(例如,int为0,布尔值为false)。 类似地,null是在声明时未初始化的任何引用类型变量的默认值。 这适用于所有类型的变量,实例变量或静态变量,不同之处在于,如果您使用局部变量而不初始化它们,编译器会警告您。
#null – Casting to other types
null不是对象,也不是类型。 这只是一个特殊值,可以分配给任何引用类型。 将null转换为任何引用类型都可以在编译时和运行时都很好,并且不会引发任何错误或异常。
// Typecasting null to String type
String myStr = (String) null;
// Typecasting to an Integer class
Integer myItr = (Integer) null;
// Typecasting to a Double class
Double myDbl = (Double) null;
这里要记住的重要一点是,只能将null分配给引用类型。 我们不能将null赋给原始变量,例如 int,double,float或boolean。 如果我们尝试这样做,那么编译器将抱怨。
// incompatible types : required int found null
int i = null;
// incompatible types : required short found null
short s = null;
// incompatible types : required byte found null
byte b = null:
#null – instanceOf operator
如果对具有空值或空文字本身的任何引用变量使用instanceof运算符,则将返回false。 我们可以在下面的代码片段中看到它的运行
public class NullInstanceOfExample {
public static void main(String[] args) {
Byte nullReferenceVariable = null;
System.out.println("Checking instanceof with Byte type variable with null stored");
if (nullReferenceVariable instanceof Byte) {
System.out.println("nullReferenceVariable is instance of Byte");
} else {
System.out.println("nullReferenceVariable is NOT an instance of Byte");
}
}
}
NullPointerException (NPE)
NullPointerException是运行时异常。 在Java中,可以将特殊的空值分配给对象引用。 当应用程序尝试使用具有空值的对象引用时,将引发NullPointerException。
为了获得对NullPointerException的更多了解,请查看以下提到的期刊,我们试图更详细地解释NPE。
[Read More: How to Handle Null Pointer Exception in Java
Conclusion
Conclusion
在Java中大约为null。 已尝试解决有关null的常见方案,包括:如何使用它,在哪里使用它以及如何将其用作引用类型变量的占位符。
切记,null是任何引用变量的默认值,并且您不能调用任何实例方法,也不能在Java中使用null引用访问实例变量。
Originally Published at Understanding null in Java - Developers Journal