Detailed explanation of the use of return statements in Java

Source: Internet
Author: User

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

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.