英文:
I have seen people using return -1 at the end of function. What does it actually mean?
问题
返回一个 -1 值代表什么意思?
英文:
I am implementing stack in java. While doing this I saw a code where a function returns -1. Like this one:
public int peek()
{
if (!isEmpty())
return arr[top];
else
System.exit(1);
return -1;
}
What does it actually mean returning a -1 value?
答案1
得分: 3
System.exit(1);
结束JVM并向操作系统返回退出状态1。
你知道,return -1;
永远不会被执行,但编译器必须强制执行,在每个控制流的末尾,只要方法在签名中声明了返回值,就必须有一个返回语句或抛出异常。
<details>
<summary>英文:</summary>
System.exit(1);
Ends the JVM and returns the exit status 1 to the OS.
You know, `return -1;` will never be executed, but the compiler has to enforce, that EVERY method, that says in the signature, that it returns something, has either a return-statement or a throw at every end of the control flow.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论