return有两个作用: 1.作为返回值,返回一个类型的值 2.结束方法的运行 有时候结束方法时会出错: //数组模拟栈,出栈代码 public int pop() { if (isEmpty()) { //System.out.println("栈空,不可出栈"); //return; //用上面两行代码报错,因为该方法有返回值,不能只写 return;需要用下面的代码替换 throw new RuntimeException("栈空,不可出栈"); } //先出栈,再 top-- int value=stack[top]; top--; return value; }