Java Basic 08 Method

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?
Insert picture description here
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:

  1. Method to encapsulate the code for easy calling
  2. Code can be reused
  3. 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.
Insert picture description here
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();
	}

}

Insert picture description here

Parametric method

grammar:

public static 返回值类型 方法名(数据类型 参数名,数据类型 参数名..){
	方法体
}

note:

  1. There can be no or any number of parameters, and each parameter is separated by a comma
  2. Formal parameters consist of data types and parameter names
  3. The parameter name cannot be repeated
  4. 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).
  5. 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);
	}

}

Insert picture description here

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);
}

Insert picture description here

x is not modified, why?

  1. int belongs to the basic data type, and the transfer is the value of the variable
  2. Equivalent to copy the value of the actual parameter to the formal parameter
  3. The modification of the formal parameter num in the method has no effect on the actual parameter x

Insert picture description here
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);
	}
}

Insert picture description here

The array passed into the method as an actual parameter has been modified. Why?

  1. For references, the parameter passed is the memory address of the object
  2. The actual parameter and the formal parameter point to the same object
  3. Modifications to formal parameters are modifications to actual parameters, because they are an object

Insert picture description here

Method overload

Why do I need to reload?
Insert picture description here
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?

  1. Multiple methods in one class
  2. Same method name
  3. 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);
	}
}

Insert picture description here

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);
	}
}

Insert picture description here

note:

  1. Variable parameters can be defined together with ordinary parameters
  2. 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

Guess you like

Origin blog.csdn.net/u013343114/article/details/112461550