Detailed explanation of the use of return statements in Java
The return Statement in Java is always closely related to the method. The return statement is always used in the method and has two functions. One is to return the value of the specified type of the method (this value is always determined ), one is the execution of the end method (only one return Statement ). Most of the Articles on return statements only introduce the return statements used in methods with return values (non-void return values. The use of return statements in the vodi return value method is rarely or not introduced. The return statement is used in a non-void return value type method. It not only returns the basic type, but also returns (including the user-defined class) objects. The following is a simple example :/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2007-12-3
* Time: 8:54:28
* Summary of the use of return statements in Java
*/
Public class TestReturn {
Public static void main (String args []) {
TestReturn t = new TestReturn ();
T. test1 ();
T. test2 ();
}
/**
* Test a return statement with no return value type
*/
Public void test1 (){
System. out. println ("--------- return Statement test without return value type --------");
For (int I = 1; I ++ ){
If (I = 4) return;
System. out. println ("I =" + I );
}
}
/**
* Test a return statement with a return value type
* @ Return String
*/
Public String test2 (){
System. out. println ("--------- return statement with return value type test --------");
Return "returns a string ";
}
} Running result: --------- No return Statement of the return value type test --------
I = 1
I = 2
I = 3
--------- Test a return statement with a return value type --------
Process finished with exit code 0