As we all know, return can only be used in functions with return types, but do functions with return values have to have return? Where can return be used in functions? This is a question to be discussed in this article.
--------------------------------------------------------------------------------
Example 1:
Class test {
Public String test (){
If (true ){
Return "";
}
Else {
Return "";
}
}
}
The above code can be compiled, but the following two examples cannot be compiled:
(1)
Class test {
Public String test (){
If (true ){
Return "";
}
}
}
(2)
Class test {
Public String test (){
If (isTrue ()){
Return "";
}
Else if (! IsTrue () {// All possibilities are included in the judgments of the two if statements, but they are still errors during compilation.
Return "";
}
}
Boolean isTrue (){
Return true;
}
}
Conclusion 1:
For (1), this is because the java compiler determines that a separate if statement is executed only when certain conditions are met. It assumes that if statements cannot be executed under any circumstances.
For (2), this is because the ability of the java compiler to fully cover all situations for the if else statement is limited to if... else (or if... else if... else), excluding if... else if.
--------------------------------------------------------------------------------
Let's look at Example 2:
Class test {
Public String test (){
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service