public static void main(String[] args) {
Stack stack=null;
System.out.println("1、"+stack.empty());
stack=new Stack();
System.out.println("2、"+stack.empty());
}
运行结果:stack没有指向对象
Exception in thread "main" java.lang.NullPointerException
at main.main(main.java:6)
注释掉System.out.println("1、"+stack.empty());运行结束是:true
总结:使用stack的empty()时,首先要给stack初始化