Java Basics approach and recursive algorithm

Source: Internet
Author: User
Tags float double modifier modifiers

I. Methods
1.1 What is a method
A method in Java is similar to a function in another language that is a piece of code to accomplish a particular function
1.2 Why to declare a method
DRY don ' t Repeat yourself (don't write duplicate code)
Principle to extract the logic that can be reused
Do not write duplicate code can save time and improve efficiency reduce code error probability convenient program testing and expansion
Implementing a relatively independent logic
To implement more complex logic
Can hide/encapsulate a specific code implementation body
1.3 Function of the method
Simplify code, improve code readability, and improve code maintainability
1.4 Declaration format for methods
Access modifier other permission modifiers return value type method name (parameter list) {
Method body
return value;
}

Access modifier other permission modifiers return value type method name (parameter list)--declaration of method
{--Method body
Method body
return value;
}
public static void Main (string[] args) {

}
Public access permissions decorated Fu Gong some
Static other permission modifiers statically
void return value type void indeterminate return values
Main Method Name
(string[] args) parameter list formal parameters
1.5 Classification of methods:
According to the method there are no parameters can be divided into two categories
1. No parameter method
2. A method of participation
1.6 Method declarations require attention:
1. Access modifier Other modifiers can be omitted
2. A complete method consists of a declaration part and a method body part
3. Method does not call do not execute
4. There is no nested definition of a peer relationship between methods and methods
5. If the method has a definite return value type Be sure to have a return statement that returns the value back to the
1.7 Method Invocation Format
Assignment invocation
Output call
called separately
Note: 1. When a method declaration has an explicit return value type, the call can be called separately using the output call assignment.
It is recommended to use an assignment call

2. You can only use separate calls when the method does not have a definite return value
Parameters in the method
When the method is called, the parameter is assigned to the formal parameter. The process is called a reference.
The formal parameter is a variable int i,char c
The actual parameter is a constant or a variable carrying a value.
The argument is to assign the value of the actual parameter to the formal parameter.
1.8 The parameters need to be noted:
1. The number and type of arguments must be consistent with the number type of the parameters
2. Data types are compatible
The return value of the 1.9 method
The RETURN keyword functions: Returns the result End method
Use of the return keyword:
1. Represents the results of a method after it has been executed
void indicates no definite return value
Use of 2.return
2.1 In case of no definite return value
Return can be written without writing
If you write return then return directly after the semicolon end
Cannot add any return value after
Function: End the entire method
2.2 Using return in a method with a return value type
Return value must be returned after
And the return value type must be consistent with the return value type of the method declaration
Function: Ends the entire method and returns the result to the caller
2.3 If a custom method has a branching structure
You need to add a return after each branch

Two. Overloaded overload of methods
2.1 Concept of method overloading
In the same class
Method names are different from the same parameter list and are not related to return values

Allow more than one method of the same name in a class as long as their arguments are different
Benefits:
The purpose of design overloading is to reduce the number of method names for the programmer to master
The program passes different parameters during the process of running
To dynamically invoke the best matching method for execution.
Precautions
What is called a different parameter list?
1. Different number of parameters
2. Different types of parameters
3. The order of the parameters is different (overloaded but basically different in development)
Best-fit principle for method invocation:
When the actual argument is passed if there are multiple overloaded methods that can receive the type
There will be a corresponding matching principle
Best match and small match to big principle
You have to use your own
You don't have to find one that you can convert.

byte short int long float double
Char
Iii.. Recursive algorithm
3! = 3*2*1--3*2!
3.1 Concept of recursive algorithm
And so on
In the method body of a method called the method itself is called a method of recursion
themselves call themselves
The recursion of a method contains an implicit loop
A piece of code is executed repeatedly, but this repetition does not need to use a looping statement to
You will not see the for and do: While



Java Basics approach and recursive algorithm

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.