As is known to all, return can only be used in functions that have return types, but functions with returned values must have returns? Where can return be used in a function? This is an issue that needs to be discussed in this article.
Class Test {
Public String test () {
if (true) {
Return "";
}
else{
Return "";
}
}
}
The above can be compiled, but the following two examples cannot be compiled:
A
Class Test {
Public String test () {
if (true) {
Return "";
}
}
}
Two
Class Test {
Public String test () {
if (IsTrue ()) {
Return "";
}
else if (!istrue ()) {//Two if the judgment includes all possibilities, but the compile-time error
Return "";
}
}
Boolean isTrue () {
return true;
}
}
Conclusion 1:
For (a), this is because the Java compiler determines that a separate if statement is executed only when a certain condition is met, and that the if does not have the ability to perform under any circumstances.
for (ii), this is because the Java compiler is limited to If...else (or If...else if...else) when the IF Else statement can fully encompass all situations, excluding if...else if.
public class Test {
String test () throws exception{
throw new Exception ()//Throw exception, jump program, abort program
}
}
Conclusion 3:
If an exception is created in a function and thrown, the function can not return a value.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.