一道有关Java null笔试题引发的思考

具体题目记不清楚了,题目问的是将一个空引用转换为本类引用然后调用方法,判断执行结果。
个人感觉这其实跟c/c++的空指针相似
直接上测试代码

public class Main {

    public static void main(String[] args) {
        try {
            ((Main) null).teststaticNull();
        } catch (NullPointerException e) {
            System.out.println("null can not invoke static methood");
        }
        try {
            ((Main) null).testnormalNull();
        } catch (NullPointerException e) {
            System.out.println("null can not invoke normal methood");
        }

    }

    private static void teststaticNull() {
        System.out.println("null can invoke static methood");
    }
    private void testnormalNull() {
        System.out.println("null can invoke normal methood");
    }
}

输出结果

null can invoke static methood
null can not invoke normal methood

打开.class文件可以看到代码变成了

public static void main(String[] args) {
        try {
            Main var10000 = (Main)null;
            teststaticNull();
        } catch (NullPointerException var3) {
            System.out.println("null can not invoke static methood");
        }

        try {
            ((Main)null).testnormalNull();
        } catch (NullPointerException var2) {
            System.out.println("null can not invoke normal methood");
        }
    }

道理很简单,static方法属于类,null调用static方法实际上是并不需要经过对象,相当于直接调用静态方法,在编译的时候就对象被擦除了。所以第一个输出是这样的。
而实例方法是属于对象的。null在堆中并没有内存,并没有方法,Jvm通过反射invoke method时找不到就出现了我们看到的空引用异常。

补充

在Java Spec里面对null的说明如下:

There is also a special null type, the type of the expression null, 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. The null reference is the only possible value of an expression of null type.The null reference can always be assigned or cast to any reference type. 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类型,他没有名字,所以不能声明null类型变量,也不能成为被转换的对象。一个null类型表达式的唯一可能值就是空引用。null引用可以被任何引用类型赋值或者转换为任何引用类型。
最佳实践:程序员可以忽略null类型,把他当成可以是任何引用类型的字面量即可。

null与instanceof
Java tutorial里面有句话
When using the instanceof operator, keep in mind that null is not an instance of anything.
null并不是任何Object的实例
所以null instanceof Object的结果永远是false。

null与==
Java Spec里面也有写
null==null返回true;
null与Map
HashMap支持null作为key,Hashtable不支持。
null最佳实践
用null去标识
1.未初始化的状态
2.终结状态(IO相关)
3.不存在对象
4.未知的值

处理空指针异常

一般方式

if(object !=  null){
    //doSomething();
}else{
    //doOtherthing();
}

虽然看起来并不怎么舒服,但是也是可以处理的。一层还算好,要是几层下来的话,代码可读性就相当差了
http://stackoverflow.com/questions/271526/avoiding-null-statements里面几种处理方式可以一试
1.Assert
2.try/catch
3.使用注解@NotNull

Java8的Optional

oracle的technetwork有篇文章介绍了用法
Tired of Null Pointer Exceptions? Consider Using Java SE 8’s Optional!
国内译文
具体用法上面文章已经很清楚了。我这边就不介绍了。

总结:其实想想都会觉得简单,也就是类方法与实例方法的区别而已,只是在笔试中心态不够好。

不论是笔试还是面试,一个自信的心态极其重要,又想起了昨天面试一道翻转链表没写出来的事了。
首先自信的心态能够保证我们的发挥不失常,再者企业同样希望自信的人才。心态很重要。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值