美文网首页
NULL-SpringMVC-Java

NULL-SpringMVC-Java

作者: ChenyuMa | 来源:发表于2017-07-12 16:22 被阅读0次

Java Null Exception 最大的原因  ------> null 使用失误

1. Java中的Null是什么?

a) Java的关键字,像public、static、final, 它是case-sensitive

b) 任何引用类型的默认值(所有object类型的引用值),像int的默认值是0,boolean的默认值是false. 

c) null 可以将其赋予任何引用类型,仅仅是一种特殊的值,

             String str =null;// null can be assigned to String

             Integer itr =null;// you can assign null to Integer also

             Double dbl =null;// null can also be assigned to Double

             String myStr = (String)null;// null can be type cast to String

             Integer myItr = (Integer)null;// it can also be type casted to Integer

             Double myDbl = (Double)null;// yes it's possible, no error

d) null不可以赋给基本类型变量,例如int、double、float、boolean。

e) 任何含有null值的包装类在Java拆箱生成基本数据类型时候都会抛出一个空指针异常

            Integer iAmNull =null;

            int i = iAmNull;     // Remember - No Compilation Error

在控制台会抛出空指针异常

f) 如果使用了带有null值的引用类型变量,instanceof操作将会返回false;

           Integer iAmNull =null;

          if(iAmNullinstanceofInteger){

                   System.out.println("iAmNull is instance of Integer");

            }else{

                   System.out.println("iAmNull is NOT an instance of Integer");

             }

说明 null 不是Integer的特定类的实例,即不是Integer的一个实例

g) null值的引用类型变量调用非静态方法,会抛出空指针异常

静态方法使用静态绑定,不会抛出空指针异常

            Demo myObject = null;

            myObject.iAmStaticMethod();

            myObject.iAmNonStaticMethod();

           }

            private static void iAmStaticMethod(){

             System.out.println("I am static method, can be called by null reference");

             }

             private void iAmNonStaticMethod(){

              System.out.println("I am NON static method, don't date to call me by null");

               }

h) 可以使用 == 或者!=操作来比较null值

           null == null

相关文章

  • NULL-SpringMVC-Java

    Java Null Exception 最大的原因 ------> null 使用失误 1. Java中的Null...

网友评论

      本文标题:NULL-SpringMVC-Java

      本文链接:https://www.haomeiwen.com/subject/lkgmhxtx.html