Detailed explanation and summary of java method

1, What is the method

In daily life, what we call method is the solution taken to solve something

Methods in java can be understood as a collection of statements, which are used to solve something or realize a function

Advantages of the method:

  • The program becomes shorter and clearer
  • It is conducive to program maintenance
  • It can improve the efficiency of program development
  • It improves the reusability of code

Naming rules for methods:

  • Must start with a letter, underscore, or $
  • Can contain a number, but cannot start with a number

2, Method and call

Access modifier return value type method name (parameter list){
    Method body
}

(1) Return value:

  • It can be understood that a method is to do something. When this thing is done, there will be only one result and the return value will be only one result

  • If the design of the method specifies the return value type, a value must be returned through the return keyword in the method

  • The return value type defined by the method return must return the return value type defined by the method. For example, if the return value is of String type, the value of return must also be of String type

(2) Parameters: parameters are divided into formal parameters and arguments

  • Formal parameter: it can be understood as the temporary variable space during method execution (defining the parameter after the method bracket)
  • Argument: it can be understood as the parameter passed in when calling the method (the parameter passed in when calling the method)

(3) Access modifier:

  • Public: public
  • Protected: protected
  • Default do not write: default
  • Private: private

example:

1. No parameter, no return value

//No parameter, no return value	
public void medth(){
   	System.out.println("This is a method with no parameters and no return value")
}
public static void main(String[] args){
    //Because the medth here has no parameters and no return value, it can be called directly without receiving
    //Calling medth will output "this is a method with no parameters and no return value"
    medth();
}

2. No parameter has return value

//No parameter has a return value. The return value type is String. All returns must also be of String type
public String medth(){
    String a="I am String Variables of type can be returned";
    return a;
}
public static void main(String[] args){
    String b=medth();
    //b here will output "I am a variable of String type, which can be returned"
    System.out.println(b);
}

3. With or without return value

//There are parameters without return value. A here is a formal parameter, which can be used in the method
public void medth(String a){
    //Here, the passed in value "I call medth method and pass this sentence to medth" will be output
    System.out.println(a)
}
//Define a main method here and call the medth method to pass parameters
//a here is an argument
public static void main(String[] args){
    String a="I call medth Method and pass this passage to medth";
    medth(a);
}

4. There are parameters and return values

//There are parameters and return values. A here is a formal parameter, which can be used in the method
public String medth(String a){
    System.out.println(a);
    //Send the a return passed in
    return a;
}
//Define a main method here and call the medth method to pass parameters
//a here is an argument
public static void main(String[] args){
    String a="I call medth Method and pass this passage to medth";
    String b= medth(a);
    //"I call medth method and pass this message to medth" will be output here
    System.out.println(b);
}

3, Method overloading

If two methods have the same method name but different parameters, it can be said that one method is an overload of the other method

Specific description of method overloading

  • Same method name
  • The parameter types and number of parameters of the method are different
  • Methods with different return types
  • Methods can have different modifiers
  • The main method can also be overloaded

example:

//Overloading is formed as long as the type or number of parameter lists are different, which is independent of the return value and access modifier
public class Test(){
    public void medth(){
        System.out.println("I have no parameters passed in, so this sentence will be output when I am called")
    }
    private void medth(int a){
        System.out.println(a)
    }
    int medth(String a){
         System.out.println(a)
    }
    protected String medth(String a,int b){
         System.out.println(a+"---"b)
             
    }
    public static void main(String[] args) {
     test t=new test();
      t.medth();
      t.medth(1);
      t.medth("I am String Parameters of type");
      t.medth("I am String Parameters of type",1);
      }
}

Execution results of the above procedures:

I have no parameters passed in, so this sentence will be output when I am called
1
 I am String Parameters of type
 I am String Parameters of type---1

4, Variable length parameter

Java 5 provides variable length parameters, that is, an ambiguous number of parameters can be used in the method definition, and different numbers of parameter calls can be used for the same method

Syntax:

Access modifier return value method name(Parameter type... Parameter name){
    Method body
}

example:

public class Test(){
public void medth(String... args){
     for(int i=0;i< args.length;i++){
          System.out.println(args[i]);
      }
}
public static void main(String[] args){
    Test t=new Test();
    t.medth("This is the first parameter","This is the second parameter");
	}
}

The results of the above procedures are as follows:

This is the first parameter
 This is the second parameter

Note:

  • A method can only have one variable parameter, otherwise an error will be reported, and the variable parameter should be the last left parameter
  • Fixed parameters can be defined in front of variable parameters, and the fixed parameter method will be used preferentially

5, Recursion (understanding)

Recursion is an algorithm that calls itself

Make a vivid analogy: we can compare "recursion" to "looking up a dictionary" ", when you look up a word and find that you still don't understand a word in the explanation of the word, you start to look up the second word. Unfortunately, there are still words you don't understand in the second word, so look up the third word, and keep looking up until you can fully understand the explanation of a word, then the recursion comes to an end, and then you start to step back and understand each word you checked before. Finally, you understand the meaning of the first word Think. (from a reply from Zhihu)

example:
Previously, we used nested for loops when printing the 99 multiplication table. Here, we will use the recursive method to implement the 99 multiplication table

public class Test(){
    //Define a method to implement recursion  
    public void medth(int i){
        if(i==1){
			System.out.println("1*1=1");
        }else{
            medth(i-1);
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
    }
    public static void main(String[] args){
        Test t=new Test();
        t.medth(9);
    }
}

Execution results of the above procedures:

1*1=1
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	

advantage:

  • The code is concise, clear, and easy to verify its correctness

Disadvantages:

  • Its operation requires more function calls. If the number of call layers is deep, additional stack processing needs to be added
  • Stack is mainly used to store stack frames. Every time a method is executed, there will be stack pressing operation. Therefore, when recursion is used, more stack frames will be generated. Recursion will affect memory and consume memory very much

Tags: Java

Posted by boinnk on Wed, 18 May 2022 18:28:54 +0300