Java Basics-Method return Values & recursion

Source: Internet
Author: User

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

Example: Write a method, function is sort, and then upside down
I'll give you a way to pass an array, you can sort the array backwards, and then you can see the results after you run the Main method output.

Method return Value:
The results are fed back at the end of the method.
The procedure and method are similar, but the procedure does not return a value.
The return value can be only one type, only one value, and cannot be returned with multiple values.
Void: no return value
public static int[] m () {} return value type declaration
The last sentence of the method must have a return statement

Method return value: is used to return a result when the method is finished running or is a feedback that the data is loaded, the return value can also pass the feedback result by reference, so there are two kinds of return values: One is a reference pass, and one is the result of the feedback parameter.

Return:
1. In the method with the return value
2. No return value in the method
3. You can not write the return statement unless you throw an exception
No code can be written after 4.return.
The execution of the 5.return represents the end of the method, there is no opportunity to add the code behind it, so there is no need to return the code that was written later.

Execution process:
Apply for a variable, call the M1 method, the 10 passed to the a,30 passed to B, and then a+b to get 40, and then return the 40 to go out, the return method is executed, the return value will be given to Num.

Personal income Tax: The calculation of the tax process into a method, and finally print after-tax wages.
Method is the method code block, is to separate it out, later useful to get the time to call directly.

Example: The cause of the error: the middle if statement less a return.

Example: Use the IF Else statement to determine if the age meets the requirements and does not meet the requirements return end method.

Method recursion:
Nested self-tuning behavior
Nested invocation of a method does not end until the last method ends
In general, this kind of code needs to be repeated, such as the table of contents, the result is suitable for the use of such nesting.

Process:
Call the value of I, it will ask I and so not equal to 1, not equal to 1 i-1, and then ask I and so not equal to 1, not equal to 1 i-i, and then ask I and so on is not equal to 1 equals 1 words feedback out, each result is feedback out, the final result is 3.

Call I of the value, ask I, etc is not equal to 1, not equal to 1 on (i-1) +i, then ask I and so not equal to 1, not equal to (i-1) +i, and then ask I etc not equal to 1, equals 1 words on the feedback result, 1+2+3, each time the result value. The final result is 6.

To remove the disk service file recursively:

Java Basics-Method return Values & recursion

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.