关于Java中null的有趣事实详细分析

2021年4月6日19:16:01 发表评论 697 次浏览

几乎所有的编程语言都以null绑定。几乎没有程序员会为null所困扰。

在Java中, null与java.lang.NullPointerException相关联。由于它是java.lang包中的类, 因此当我们尝试执行一些带有或不带有null的操作时有时会调用它, 有时我们甚至不知道它的发生位置。

以下是每个Java程序员都应了解的有关Java中null的一些重要信息:

1. null区分大小写:在Java中, null是文字, 因为关键字是区分大小写在Java中, 我们不能像在C语言中那样写NULL或0。

public class Test
{
     public static void main (String[] args) throws java.lang.Exception
     {
         // compile-time error : can't find symbol 'NULL'
         Object obj = NULL; 
          
         //runs successfully
         Object obj1 = null ; 
     }
}

输出如下:

5: error: cannot find symbol
 can't find symbol 'NULL'
                 ^
   variable NULL
 class Test
1 error

2.参考变量值:Java中的任何引用变量的默认值均为null。

public class Test
{
     private static Object obj; 
     public static void main(String args[])
     {
         // it will print null;
         System.out.println( "Value of object obj is : " + obj);
     } 
}

输出如下:

Value of object obj is : null

3.空类型:

与常见的误解不同, null不是Object也不不是类型。它只是一个特殊值, 可以分配给任何引用类型, 也可以将强制类型转换为任何类型

例子:

// null can be assigned to String
    String str = null; 
    
    // you can assign null to Integer also
    Integer itr = null; 
    
    // null can also be assigned to Double
    Double dbl = null; 
        
    // null can be type cast to String
    String myStr = (String) null; 
    
    // it can also be type casted to Integer
    Integer myItr = (Integer) null; 
    
    // yes it's possible, no error
    Double myDbl = (Double) null;

4.自动装箱和拆箱:在自动装箱和拆箱操作期间, 如果将空值分配给原始装箱的数据类型, 则编译器仅引发Nullpointer异常错误。

public class Test
{
     public static void main (String[] args) throws java.lang.Exception
     {
             //An integer can be null, so this is fine
             Integer i = null ;
              
             //Unboxing null to integer throws NullpointerException
             int a = i;
     }
}

输出如下:

Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:6)

5. instanceof运算符:

java instanceof运算符用于测试对象是否是指定类型(类, 子类或接口)的实例。在运行时, 如果Expression的值不为null, 则instanceof运算符的结果为true。

这是instanceof操作的重要属性, 使其对于类型转换检查很有用。

public class Test
{
     public static void main (String[] args) throws java.lang.Exception
     {
         Integer i = null ;
         Integer j = 10 ;
              
         //prints false
         System.out.println(i instanceof Integer);
          
         //Compiles successfully
         System.out.println(j instanceof Integer);
     }
}

输出如下:

false 
true

6.静态方法与非静态方法:我们不能对具有空值的引用变量调用非静态方法, 它将抛出NullPointerException, 但我们可以对具有空值的引用变量调用静态方法。由于静态方法是使用静态绑定进行绑定的, 因此它们不会引发Null指针异常。

public class Test
{             
     public static void main(String args[])
     {
         Test obj= null ;
         obj.staticMethod();
         obj.nonStaticMethod();                             
     }
      
     private static void staticMethod()
     {
         //Can be called by null reference
         System.out.println( "static method, can be called by null reference" );
          
     }
          
     private void nonStaticMethod()
     {
         //Can not be called by null reference
         System.out.print( " Non-static method- " );
         System.out.println( "cannot be called by null reference" );
          
     }
  
}

输出如下:

static method, can be called by null referenceException in thread "main" 
java.lang.NullPointerException
    at Test.main(Test.java:5)

7. ==和!=在Java中, 允许进行比较且不等于运算符, 并且该参数可以为null。这对于用Java中的对象检查null很有用。

public class Test
{             
     public static void main(String args[])
     {
      
     //return true;
     System.out.println( null == null );
      
     //return false;
     System.out.println( null != null );
          
     }
}

输出如下:

true
false

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: