Return statements in the Java language

Source: Internet
Author: User
Tags functions return string throw exception
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.



--------------------------------------------------------------------------------


Example one:

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.



--------------------------------------------------------------------------------


Let's look at the second example:

Class Test {
Public String test () {
while (true) {
Return "";
}
}
}
The above can be compiled, but the following does not work:

Class Test {
Public String test () {
while (IsTrue ()) {
Return "";
}
}
Boolean isTrue () {
return true;
}
}

Conclusion 2:
This is because the compiler believes that the while statement has the ability to execute in any case, but only if the argument is true.



--------------------------------------------------------------------------------


See Example three:

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.



--------------------------------------------------------------------------------


Knowing the above situation, you can use the return skillfully and freely.


Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.