Java小白踩坑录 - Java有关null的几件小事

简介: Java小白踩坑录 - Java有关null的几件小事

Java 中的 null 看着很简单,也容易被很多人忽略,有时候也让人不知所措。

Java 中的空 null

我们先看几段代码吧。

1. 例一:null 的对象性

public class NullTest {
 public static void greet() {
  System.out.println("Hello world!");
 }
 public static void main(String[] args) {
  ((NullTest) null).greet();
 }
}

上面的程序看起来似乎应该抛出 NullPointerExceptioin 异常,因为其 main 方法是在常量 null 上调用 greet 方法,而你是不可以在 null 上调用方法的,对吗?

其实编译和运行都没有问题。运行结果为:

Hello world!

2. 例二:null 的初始化

public static void main(String[] args) {
 String str=null;
 Integer in=null;
 Double dou=null;
 String str1=(String)null;
 Integer in1=(Integer)null;
 Double dou1=(Double)null;
 int in2=null;
 int in3=(int)null;
}

image.png

发现 null 可以初始化引用类型,也可以转换为任意的引用类型。但不能给基本类型赋值,或者转换为基本类型。

3. 例三:null 的相等性

public static void main(String[] args) {
  System.out.println(null==null);
  System.out.println(null!=null); 
  System.out.println(Double.NaN==Double.NaN);
  System.out.println(Double.NaN!=Double.NaN);
}

结果该是什么呢?

true
false
false
true

4. 例四:null 不是引用类型

public static void main(String[] args) {
  Integer in=null;
  if(in instanceof Integer) {
    System.out.println("null is integer");
  }else {
    System.out.println("null is not integer");
  }
}

结果是:

null is not integer

5. 例 5:不可传递

1. public static void main(String[] args) {
2.  Integer i=null;
3. int k=i;
4.  System.out.println(k);
5. }

报错:

1. Exception in thread "main" java.lang.NullPointerException
2. NullTest.main(NullTest.java:6)

6. 例 6:null 的数组

public static void main(String[] args) {
 String[] arr1={"abc","123",null,"sky"};
 boolean flag=false;
 for (String s1 : arr1) {
  if(s1.equals("sky")) {
    flag=true;
    break;
  }
 }
 System.out.println(flag);
}

运行时报错

1. Exception in thread "main" java.lang.NullPointerException
2. at NullTest.main(NullTest.java:8)

修改成

public static void main(String[] args) {
 String[] arr1={"abc","123",null,"sky"};
 boolean flag=false;
 for (String s1 : arr1) {
  if("sky".equals(s1)) {//对比前后顺序
    flag=true;
    break;
  }
 }
 System.out.println(flag);
}

就没有问题了。

追根到底

JSL 3.10.7 定义了null

The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters.

JSL 4.1 做了补充:

1.There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

注:null 是一种特殊类型,它的表达式为 null,但没有名称。因为 null 类型没有名称,故不能声明一个 null 类型(如 private null a),也不能将一个类型转为 null 类型。

2.The null reference is the only possible value of an expression of null type.

注:使用 null 类型的唯一方式是使用 null 引用(如 private Integer a = null);

3.The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).

注:空引用可以赋值给其他任意类型,如 String,Integer,Class 等等。

4.In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

注:其实,程序开发者可以忽略 null 类型,仅仅将它当作一种可以赋值给其他任意类型的特殊引用。

目录
相关文章
|
2天前
|
安全 Java 开发者
Java一分钟之-Optional类:优雅处理null值
【5月更文挑战第13天】Java 8的`Optional`类旨在减少`NullPointerException`,提供优雅的空值处理。本文介绍`Optional`的基本用法、创建、常见操作,以及如何避免错误,如直接调用`get()`、误用`if (optional != null)`检查和过度使用`Optional`。正确使用`Optional`能提高代码可读性和健壮性,建议结合实际场景灵活应用。
21 3
|
2天前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
2天前
|
Java
java Map删除值为null的元素
java Map删除值为null的元素
|
6月前
|
Java
java servlet 文件上传 req.getPart null 返回空值
java servlet 文件上传 req.getPart null 返回空值
75 0
|
2天前
|
Java
JAVA——List中剔除空元素(null)的三种方法汇总
JAVA——List中剔除空元素(null)的三种方法汇总
|
7月前
解决HTTP状态 500 - 内部服务器错误java.lang.NumberFormatException: null~
解决HTTP状态 500 - 内部服务器错误java.lang.NumberFormatException: null~
122 0
|
2天前
|
Java
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
36 1
|
2天前
|
SQL JSON Java
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
44 0
|
2天前
记录以下出现:java.io.IOException: (null) entry in command string: null ls -F E:\file\a.txt 情况怎么办?
记录以下出现:java.io.IOException: (null) entry in command string: null ls -F E:\file\a.txt 情况怎么办?
48 0
|
6月前
|
安全 关系型数据库 MySQL
java.sql.SQLException: null,message server: Host ora-rac2 is blocked because of many
java.sql.SQLException: null,message server: Host ora-rac2 is blocked because of many