Chapter 7 method for getting started with Java
7-1 how to define methods in Java
The so-called method is an orderly combination of codes used to solve a type of problem and is a functional module.
Generally, the syntax for defining a method is:
Where:
1. access modifier: the permitted range of a method, which can be public, protected, private, or even omitted. public indicates that the method can be called by any other code, the usage of other modifiers will be detailed in the following sections.
2. return Value Type: Type of the method return value. If the method does not return any value, the return value type is specified as void. If the method has a return value, the type of the return value must be specified, use the return statement to return the value of the method weight.
3. Method Name: name of the defined method, which must use a valid identifier.
4. parameter List: a list of parameters passed to a method. Multiple parameters can be included. Multiple parameters are separated by commas (,). Each parameter is composed of the parameter type and parameter name and is separated by spaces, methods can be divided into four categories based on whether the method has a parameter or a returned value:
? Method of returning value without Parameters
? Return Value Method without Parameters
? Method without return value
? Return Value Method with Parameters
7-2 Use of methods without parameters and return values in Java
If a method does not contain a parameter and has no return value, it is called a method without a parameter or a return value.
The method can be used in two steps:
Step 1: Define the Method
For example, the following code defines a method named show with no parameters and no return value. The operation is to output "welcome to imooc ."
Note:
1. Place the method body in a pair of braces to implement specific operations
2. The method name is mainly used when calling this method. Note the naming rules. Generally, the first letter is lowercase, And the other letters are capitalized.
Second, call Methods
When you need to call a method to execute an operation, you can first create the class object, and then implement it by the object name. Method Name.
For example, in the following code, we create an object named hello and call the show () method of the object to output information.
7-2 Use of the return value method without parameters in Java
If a method does not contain parameters but has returned values, it is called a method without parameters and return values.
For example, the following code defines a method named calSum with no parameters, but the return value is an int type method. The operation is to calculate the sum of the two numbers and return the result.
In the calSum () method, the return value type is int. Therefore, you must use return to return an integer in the method body.
When calling a method with a return value, you must note that a result is returned after the method is executed. Therefore, when calling a method with a return value, the returned value is generally received and processed. For example:
Running result: the sum of the two is 17.
Small traps that cannot be ignored ":
1. If the return type of the method is void, the return value cannot be used in the method!
2. A method can return a maximum of one value, but cannot return multiple values.
3. the type of the method return value must be compatible. For example, if the return value type is int, the String type value cannot be returned.
7-3 Use of the method without returning values in Java
Sometimes the execution of a method depends on certain conditions. In other words, you need to provide additional information for a method to complete a specific function. For example, in real life, rice cookers can achieve the "Cooking" function, but the premise is that we must provide food. If we do not provide anything, it is really "A clever man is hard to cook without rice. You can add a parameter list to the method to receive external incoming data information. A parameter can be of any basic or reference type.
Let's first look at a method with parameters but no return value:
The code above defines a show method with a parameter name to output welcome messages.
The syntax for calling a method with parameters is similar to that for calling a method without parameters, but the actual parameter value must be input during the call.
For example:
Running result: Welcome, love class!
Most of the time, we call parameters when defining a method a form parameter to define the number and type of parameters to be passed in a method, and call parameters when calling a method a real parameter, is the value passed to the method to be processed.
Problems that must not be ignored:
1. When calling the method with parameters, you must ensure that the quantity, type, and order of the real parameters correspond to the parameters one by one.
2. When calling a method, you do not need to specify the data type for the real parameter, as shown in figure
3. method parameters can be basic data types, such as int and double, or reference data types, such as String and array.
4. When there are multiple method parameters, multiple parameters are separated by commas (,).
7-3 Use of the return value method with parameters in Java
If a method contains both parameters and return values, it is called a method with parameters and return values.
For example, the following code defines a show method with a parameter name. After the method is executed, a String type result is returned.
Call the method with parameters and return values:
Running result: Welcome, love class!
7-4 method overloading in Java
Q: What is method overloading?
A: If the same class contains two or more methods with the same name, number of method parameters, sequence, or type, it is called method overload, this method can also be called overloaded. The names of the four methods are show, but the parameters of the methods are different. Therefore, all methods are overloaded:
Q: How do I differentiate which overload method is called?
A: When you call a method that is overloaded, Java determines which method to call based on the number and type of parameters. The method that completely matches the parameter will be executed. For example:
Running result:
Basis for determining method overloading:
1. It must be in the same class
2. The method name is the same
3. The number, sequence, or type of method parameters are different.
4. It is irrelevant to the modifier or return value of the method.