Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451
Preface
Method is the content that must be mastered in Java programming, the content that this article will introduce is as follows
- Overview of the method
- No-parameter method
- Parametric method
- The difference between basic types and reference type parameters
- Method overload
- Method with return value
- variable parameter
Overview of the method
What is the method?
Method is a piece of code block, which can be called and executed when needed. It is also called function in some other languages (C, JavaScript, etc.).
What are the benefits of the method?
Long long ago... Everyone used matches to light the fire, and then discarded them. When needed, they had to take new matches to light. Later, lighters appeared, which were reusable, convenient and environmentally friendly, so matches were eliminated.
Benefits of the method:
- Method to encapsulate the code for easy calling
- Code can be reused
- Code is easy to maintain
Method related concepts:
- parameter
- return value
Comparing the method to a rice cooker, then water and rice are the parameters. After the rice cooker is passed into the rice cooker to execute the logic of cooking, the return value is rice.
Methods are divided into parameters:
- Parametric method
- No-parameter method
No-parameter method
grammar:
public static 返回值类型 方法名(){
方法体;
}
Note: If there is no return value, the return value type is void (empty)
public class DemoMethod {
/**
* 打印一个5*5的正方形
*/
public static void printSquare(){
for(int i = 0;i < 5;i++){
for(int j = 0;j < 5;j++){
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
//调用方法
printSquare();
}
}
Parametric method
grammar:
public static 返回值类型 方法名(数据类型 参数名,数据类型 参数名..){
方法体
}
note:
- There can be no or any number of parameters, and each parameter is separated by a comma
- Formal parameters consist of data types and parameter names
- The parameter name cannot be repeated
- The parameters declared when the method is defined are called formal parameters (formal parameters)
. The parameters passed in when the method is called are called actual parameters (actual parameters). - Once the formal parameters are defined, the corresponding type and number of actual parameters must be passed in when calling
Parameterized method call
方法名(实参); //实参可以是数值、变量或表达式。
public class DemoMethod2 {
/**
* 打印一个长方形
*/
public static void printRectangle(int rows,int cols){
for(int i = 0;i < rows;i++){
for(int j = 0;j < cols;j++){
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
//调用方法
printRectangle(4,8);
}
}
The difference between reference types and basic data types as parameters
Basic types (byte, short, char, int, long, float, double, boolean)
reference types (array, class, interface)
Parameters of basic data types:
public static void changeNum(int num){
num += 10;
System.out.println("num="+num);
}
public static void main(String[] args) {
int x = 100;
changeNum(x);
System.out.println("x="+x);
}
x is not modified, why?
- int belongs to the basic data type, and the transfer is the value of the variable
- Equivalent to copy the value of the actual parameter to the formal parameter
- The modification of the formal parameter num in the method has no effect on the actual parameter x
Array as parameter:
public static void changeArray(int[] array){
for(int i =0;i < array.length;i++){
array[i] += 10;
}
}
public static void main(String[] args) {
int[] numbers = {1,2,3,4,5};
changeArray(numbers);
for(int num : numbers){
System.out.println(num);
}
}
The array passed into the method as an actual parameter has been modified. Why?
- For references, the parameter passed is the memory address of the object
- The actual parameter and the formal parameter point to the same object
- Modifications to formal parameters are modifications to actual parameters, because they are an object
Method overload
Why do I need to reload?
The rice cooker only needs one cook method, which is passed through different parameters, automatically calls different logics, and returns different results.
What is overloading?
- Multiple methods in one class
- Same method name
- Different parameters (type, number)
public class DemoMethod5 {
public static void cook(){
System.out.println("什么都没放啊!");
}
public static void cook(String type,int water){
System.out.printf("煮放了%d水的%s汤\n",water,type);
}
public static void cook(String type,int solt,int water){
System.out.printf("煮放了%d水、%d盐的%s汤\n",water,solt,type);
}
public static void main(String[] args) {
cook();
cook("老母鸡",200);
cook("老鸭",200,10);
}
}
Method with return value
If you need to get a result after the method operation, you need to return a value.
If you need a return value, add a specific type before the method name
public static 返回值类型 方法名(数据类型 参数名~~~){
...
return 结果;
}
Note: Once the return value type is defined, the method must use return to return the result of that type
public class DemoMethod6 {
public static int sum(int num1,int num2){
return num1 + num2;
}
public static void main(String[] args) {
int result = sum(100,200);
System.out.println("result is "+result);
}
}
Exercise:
Complete the four methods of addition, subtraction, multiplication, and division. The parameters are two double numbers, and the calculation results are returned.
variable parameter
Variable parameters can be adapted to the situation where the number of parameters is uncertain.
public static 返回值类型 方法名(数据类型... 参数名){
方法名
}
transfer:
方法名();
方法名(参数1);
方法名(参数1,参数2);
...
Variable parameters can be treated as an array of uncertain length when processing
public class DemoMethod7 {
public static int sum(int... numbers){
int sum = 0;
for(int i = 0;i < numbers.length;i++){
sum += numbers[i];
}
return sum;
}
public static void main(String[] args) {
int res0 = sum(100);
System.out.println("result is "+res0);
int res1 = sum(100,200);
System.out.println("result is "+res1);
int res2 = sum(100,200,300);
System.out.println("result is "+res2);
}
}
note:
- Variable parameters can be defined together with ordinary parameters
- Variable parameters should be defined at the end
End
This article is over, I don’t know if you have mastered the method,
let’s leave it a homework: 1. Use the method to achieve the maximum value of the array
2. Use the method to achieve the binary search of the array
3. Use the method to achieve the selection and sorting of the array
If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary