Use of java methods

Keywords: Java

1. Basic usage of the method

1.1 what is a method

1. A method is a code fragment. It is similar to the "function" in C language
2. Significance of the method:
(1) It is a modular organization code (when the code scale is complex)
(2) The code can be reused, and a code can be used in multiple locations
(3) Make the code easier to understand
(4) Directly call the existing methods for development, and there is no need to build wheels repeatedly

1.2 method definition syntax

1. Basic grammar

// Method definition
public static Method return value method name([Parameter type parameter ...]){
 Method body code;
 [return Return value];
}
// Method call
 Return value variable = Method name(Argument...);

2. Code example: implement a method to add two integers

class Test {
 public static void main(String[] args) {
 int a = 10;
 int b = 20;
        
        // Method call
 int ret = add(a, b);
 System.out.println("ret = " + ret);
 }
    // Definition of method
 public static int add(int x, int y) {
 return x + y;
 }
}
// results of enforcement
ret = 30

3. Precautions:
(1)public and static keywords have specific meanings here;
(2) When a method is defined, there may be no parameters. Each parameter must specify a type;
(3) When defining a method, the return value can also be null. If there is no return value, the return value type should be written as void;
(4) The parameters when a method is defined are called "formal parameters", and the parameters when a method is called are called "arguments";
(5) The method definition must be in the class, and the code can be written above or below the calling position;
(6) There is no concept of "function declaration" in Java

1.3 method call execution process

1. Basic rules
(1) When defining a method, the code of the method will not be executed. It will only be executed when calling
(2) When the method is called, the argument is assigned to the formal parameter
(3) After the parameter is passed, it will be executed to the method body code
(4) When the method is executed (encounter the return statement), it is executed. Return to the method call position and continue to execute
(5) A method can be called multiple times
2. Code example
(1) Calculate the sum of two integers:

class Test {
 public static void main(String[] args) {
 int a = 10;
 int b = 20;
 System.out.println("Before the method is called for the first time");
 int ret = add(a, b);
 System.out.println("After the first method call");
 System.out.println("ret = " + ret);
 System.out.println("Before the second method call");
 ret = add(30, 50);
 System.out.println("After the second method call");
 System.out.println("ret = " + ret);
 }
 public static int add(int x, int y) {
 System.out.println("In calling method x = " + x + " y = " + y);
 return x + y;
 }
}
// results of enforcement
 Before a method call
 In calling method x = 10 y = 20
 After the first method call
ret = 30
 Before the second method call
 In calling method x = 30 y = 50
 After the second method call
ret = 80

(2) Calculate 1! + 2! + 3! + 4! + 5!

class Test {
 public static void main(String[] args) {
 int sum = 0;
 for (int i = 1; i <= 5; i++) {
 sum += factor(i);
 }
 System.out.println("sum = " + sum);
 }
 public static int factor(int n) {
 System.out.println("calculation n Factorial of! n = " + n);
 int result = 1;
 for (int i = 1; i <= n; i++) {
 result *= i;
 }
 return result;
 }
}
// results of enforcement
 calculation n Factorial of! n = 1
 calculation n Factorial of! n = 2
 calculation n Factorial of! n = 3
 calculation n Factorial of! n = 4
 calculation n Factorial of! n = 5
sum = 153

Use method, avoid using double loop, make the code simpler and clearer

1.4 relationship between arguments and formal parameters

1. Code example: exchange two integer variables

class Test {
 public static void main(String[] args) {
      int a = 10;
      int b = 20;
      swap(a, b);
 System.out.println("a = " + a + " b = " + b);
 }
 public static void swap(int x, int y) {
       int tmp = x;
       x = y;
        y = tmp;
 }
}
// Operation results
a = 10 b = 20

2. Cause analysis:
The code just now did not complete the data exchange. For the basic type, the formal parameter is equivalent to the copy of the actual parameter, that is, the value passing call

nt a = 10;
int b = 20;
int x = a;
int y = b;
int tmp = x; x = y; y = tmp;

As you can see, the modifications to x and y do not affect a and b
3. Solution: pass reference type parameters (such as array to solve this problem)

class Test {
 public static void main(String[] args) {
 int[] arr = {10, 20};
 swap(arr);
 System.out.println("a = " + arr[0] + " b = " + arr[1]);
 }
 public static void swap(int[] arr) {
 int tmp = arr[0];
 arr[0] = arr[1];
 arr[1] = tmp;
 }
}
// Operation results
a = 20 b = 10

Posted by jswinkelman on Mon, 04 Oct 2021 16:13:43 -0700