java method understanding
1. Method
1.1 overview
A method can also be called a function. A method is a piece of code with functions. This piece of code can be placed separately in a {} to give the code a name, which is the method name.
This code can be called through the method name without repeated writing, so the method can improve the reusability of the code.
Methods in Java cannot exist independently. All methods must be defined in classes.
1.2 form
(The contents in square brackets are not necessary. You can decide whether to add them according to the actual situation) Modifier return value type/void Method name ([Parameter type parameter name 1,Parameter type parameter name 2]){ Method body sentence [return Return value;] }
The above syntax format is described as follows:
a) Modifier: there are many modifiers for methods, which can limit access permissions. Now use public static (which will be described in detail later)
b) Return value type: used to limit the data type of the method return value. If the method does not need a return value, it can be declared as void.
c) Method name: used to call a method (the method name is also an identifier and needs to conform to the syntax of the identifier)
d) Parameters:
Actual parameter: the parameter that actually participates in the operation
Formal parameter: the parameter used to receive actual parameters in the method definition
e) Parameter type: the data type of the parameter
f) Parameter name: variable name
g) Formal parameter list: when declaring a method, you can declare 0 or more parameters. If several parameters are declared, you need to pass several parameters when calling the method. Formal parameter list refers to the parameter type, parameter order and number of parameters of a method
h) Method body sentence: code to complete the function
i) Return: used to end the method in advance or return the result of the method to the caller. If the method does not need to return a value, return is not written.
1.3 exercise 1: Method Invocation
package com.tedu.function; public class HelloFunction { public static void main(String[] args) { //The method can be called using the method name, and the program will jump to the method to execute its internal code myFirstMethod(); //The execution method will not affect the execution of the following contents System.out.println("What follows"); System.out.println("What follows"); System.out.println("What follows"); System.out.println("What follows"); } /* * The modifier is currently public static * If there is no return value void, if there is a return value, write the type of the return value * The method name is to give a name to the method, which must be followed by () * ()Parameters can be written or not, which is determined according to the actual situation * {} Write the code executed when calling the method */ public static void myFirstMethod() { System.out.println("This is my first method"); } }
Summary:
a) Execute the code in order
b) When a method call is encountered, the corresponding method is executed
c) After the called method is executed, return to the calling location, and then execute the following code
1.4 exercise 2: method parameters
If the formal parameter declaration is included when the method is declared, the parameter values must be specified for these formal parameters when the method is called. The parameter values actually passed to the formal parameters when the method is called are also called arguments.
Method parameter transmission example 1: declare a method, receive two parameters, namely, the length and width of the rectangle, calculate the area of the rectangle and print it to the console
package com.tedu.function; public class HelloFunction2 { public static void main(String[] args) { //Because the length and width are uncertain, you can pass the length and width parameters to the method when calling the method method2(23,3); } //Define a method named method2 whose return value is void //Find the length * width of the rectangular area //method2(int x,int y) means that two parameters X and y need to be passed to the method2 method //Once a parameter is defined within a method, it must be passed public static void method2(int x,int y) { int area=x*y; System.out.println("The area of the rectangle is:"+area); } }
Method parameter transmission example 2: declare a method, receive a positive integer value, calculate the sum of all integers from 1 to the positive integer (e.g. 100), and print the calculated sum to the console.
package com.tedu.function; public class HelloFunction3 { public static void main(String[] args) { getSum(100); } //Define a getSum method with a return value of void //Find the sum of all integers from 1 to an integer public static void getSum(int num) { //Performs the sum of all integers between 1-positive integers int sum=0; for(int i=1;i<=num;i++) { sum+=i; } System.out.println(sum); } }
1.5. Exercise 3: return value
If the caller needs the result of the method execution to return to the location of the calling method after calling a method, the return value can be added to the method.
Transform the rectangular area obtained in the getArea method defined above and the sum obtained in the sum method to return to the caller in the form of return value.
The getArea method is modified as follows:
package com.tedu.function; public class HelloFunction2 { public static void main(String[] args) { int a=23; int b=3; //Because the length and width are uncertain, you can pass the length and width parameters to the method when calling the method int s=method2(23,3); //You need to get the area of the rectangle and add units to the area System.out.println("Area is"+s+"square meter"); } //Define a method named method2 whose return value is void //Find the length * width of the rectangular area //method2(int x,int y) means that two parameters X and y need to be passed to the method2 method //Once a parameter is defined within a method, it must be passed //If you need to return the area to the method call location, you need to return the area to void, //Change to the type of the returned value. The returned value is area and the type is int public static int method2(int x,int y) { int area=x*y; //Return value return area; } }
The transformation of getSum method is as follows:
package com.tedu.function; public class HelloFunction3 { public static void main(String[] args) { int sum = getSum(100); //Output and System.out.println("Back:"+sum); } //Define a getSum method with a return value of void //Find the sum of all integers from 1 to an integer public static int getSum(int num) { //Performs the sum of all integers between 1-positive integers int sum=0; for(int i=1;i<=num;i++) { sum+=i; } return sum; } }
2. Method overload
2.1. What is method overloading
Suppose an add method is defined in the program to sum numbers, but since the number and type of numbers involved in the summation are uncertain, different methods should be designed for different situations. Of course, methods with different names can be declared to complete it. However, if the functions are the same, but the number or type of parameters are different, it is recommended to use method overloading.
The so-called method overloading means that multiple methods with the same name can be defined in a program, but each method is required to have different parameter types or parameter numbers.
2.2 overload example of method
The summation function is realized in the form of method overload
package com.tedu.overload; public class TestOverLoad { public static void main(String[] args) { //Find the sum of two integers int sum=add(1,3); System.out.println(sum); //Find the sum of three integers add1 int sum1 = add(1,3,5); System.out.println(sum1); //Find the sum of two double floating-point numbers add2 double sum2 = add(1.2,3.4); System.out.println(sum2); } public static int add(int a,int b) { return a+b; } public static int add(int a,int b,int c) { return a+b+c; } public static double add(double a,double b) { return a+b; } }
Three add methods with the same name are defined in the above code, but their parameter numbers or parameter types are different, resulting in method overloading.
When the main function calls the add method, you can determine which overloaded method to call by the number and type of parameters transmitted.
For example, add (30, 40) calls the add method of summing two integers, and add (3.5, 4.5) calls the sum method of two floating-point numbers.
Summary: (multiple choice questions) there are two methods. How to judge whether these two methods are method overloading (a,b,c)
a) the method names of the two methods must be the same
b) the parameter types of the two methods are different
c) the number of parameters of the two methods is different
d) the return values of the two methods are different
Note: method overloading has nothing to do with the return value of the method!