On section describes the flow control statements, a complex business logic will be a lot of java code components, it contains many features. For example, a shopping service, contains selected merchandise, orders, payments and other functions, if the code is written to these features together, will look very bloated, very good readability. java provides a way to solve this problem is to encapsulate a function. In the previous example is a main method, main entrance java program method, the method can call the method.
Methods Format:
Modifier return type method name (parameter list) { ... return result; } modifiers: such as public static Return Value Type: data type of the result of running the method, the method will return the results of the list of parameters: the required calculation method parameters passed when calling the method return : return a result after performing the method, the method proceeds to return , the end of the overall method of operating }
Example:
public static void main(String[] args) { System.out.println(getSum(1,2)); } public static int getSum(int a,int b) { return a + b; }
getSum is defined by calculation a, b the addition result of the method, the main function is called, a, b are parameters of the method, as modifiers public, static method is defined as a static method, int return type of the method, the return value is described int type, corresponding to the type of return to return. The main function void return type, neither value is returned, there is no return, direct output to the console.
The concept of class will explain in the next article, modifiers for permission in advance to find out.
About two methods commonly used concepts, methods and overloaded method overrides