我想知道为什么以下代码在Java中是允许的,而不会出现编译错误?在我看来,这段代码没有返回任何String
,从而破坏了方法签名。有人能解释一下我错过了什么吗?
public class Loop {
private String withoutReturnStatement() {
while(true) {}
}
public static void main(String[] a) {
new Loop().withoutReturnStatement();
}
}
发布于 2014-08-06 15:28:27
private String withoutReturnStatement() {
while(true) {
// you will never come out from this loop
} // so there will be no return value needed
// never reach here ===> compiler not expecting a return value
}
要获得更多说明,请尝试以下命令
private String withoutReturnStatement() {
while(true) {}
return ""; // unreachable
}
上面写着unreachable
语句
https://stackoverflow.com/questions/25154495
复制相似问题