Java Experience Sharing---Return statement usage _java

Source: Internet
Author: User

1, return statement function: A, returns a value, this value can be any type. b, return the program to the operating system (that is, terminate the program)
2, in Java for a function, regardless of whether there is a return value type, can be accompanied by returns statements.
The difference, however, is whether the return statement returns a value (depending on the return value type of the function).

A, if the function has a return value type (that is, the return value type is not void), you must have a return statement that returns the corresponding type value.
b, if the function does not have a return value (that is, the return value type is void), returns in the function (if there is a returns statement!). cannot be appended with any variables. (There can be no return statement in the function in this case, but returns can only be used as a function of returning the operating system, if any.) )
For example:

1, have return value

Copy Code code as follows:

public int Getage ()
{
return age; Returns the value of the variable age of type int

}

2, no return value//function without returns statement

Copy Code code as follows:

public void Putage ()
{
System.out.println (age);

}

3, return to the operating system//function has no return value, but with returns statement

Copy Code code as follows:

public void put (int a) {

if (a > 0)
Return Return statement without returning value, the function is to quit the program running
Else
System.out.println ("FASFSA");
}

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.