故事背景

---摩诃般若波罗蜜多心经:

观自在菩萨,行深般若波罗蜜多时,照见五蕴皆空,度一切苦厄。舍利子,色不异空,空不异色;色即是空,空即是色。受想行识,亦复如是。舍利子,是诸法空相,不生不灭,不垢不净,不增不减。是故空中无色,无受想行识,无眼耳鼻舌身意,无色声香味触法,无眼界,乃至无意识界,无无明,亦无无明尽,乃至无老死,亦无老死尽。无苦寂灭道,无智亦无得。以无所得故,菩提萨埵。依般若波罗蜜多故,心无挂碍,无挂碍故,无有恐怖。远离颠倒梦想,究竟涅磐,三世诸佛,依般若波罗蜜多故,得阿耨多罗三藐三菩提。故知般若波罗蜜多是大神咒,是大明咒, 是无上咒,是无等等咒,能除一切苦,真实不虚。故说般若波罗蜜多咒,即说咒曰:揭谛揭谛,波罗揭谛,波罗僧揭谛,菩提娑婆诃。

色即是空,空即是色---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;
    }
色即是空,空即是色---java有关null的几件小事

 

发现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");
        }
    }

你猜会打印出什么?

色即是空,空即是色---java有关null的几件小事

 

结果是:

null is not integer

5.例5:不可传递

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

报错:

Exception in thread "main" java.lang.NullPointerException

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);
    }

运行时报错

Exception in thread "main" java.lang.NullPointerException

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(s1.equals("sky")) {
            flag=true;
            break;
        }
        }
        System.out.println(flag);
    }

就没有问题了。

追根到底

JSL3.10.7定义了null

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

JSL4.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.

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

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

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.

参考资料

【1】https://baike.baidu.com/item/%E8%89%B2%E5%8D%B3%E6%98%AF%E7%A9%BA/6210?fr=aladdin

【2】https://docs.oracle.com/javase/specs/jls/se12/html/jls-4.html#jls-4.1

【3】https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-3.10.7

【4】java解惑

posted on 2019-10-12 18:08  一天不进步,就是退步  阅读(567)  评论(0编辑  收藏  举报