我见过人们在函数末尾使用 return -1。这实际上是什么意思?

huangapple go评论98阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年8月16日 19:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63436417.html
匿名

发表评论

匿名网友

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

确定