java中的null值是可以强制转换为任何java类型的,就像(String)null是合法的,但null强转后的是无效对象,其返回值还是为null,但是例如下面这种情况

public class NULL {

    public static void main(String[] args) {
        ((NULL)null).haha();
    }

    public static void haha(){
        System.out.println("haha");
    }
}

输出为

haha

原因是static方法的调用是和类名绑定的,不借助对象进行访问,所以能够正确输出,如果没有加上static修饰,就会报空指针的错误了

相关文章:

  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2022-02-23
  • 2021-08-03
  • 2021-10-24
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2021-12-23
  • 2022-12-23
相关资源
相似解决方案