JavaSE foundation-(5) Java method functions

table of Contents

1. Method overview and format description

Second, the method of attention

Three, method overload


 

The method in Java is similar to the function of C and C++, which is the code block that realizes a certain function.

But because there are no independent functions outside the class in Java, these functions are all described as methods of a certain class.

Therefore, it is more appropriate to call methods in Java. A function is completed by calling methods in classes.

Below we introduce the method in Java:

 

1. Method overview and format description

What is a method is the same as the function described in C and C++.

For some commonly used, more complex ones can be packaged as a whole code block,

We put them in curly braces, and then give this code block a name to make it easier to call the method elsewhere.

The method enhances the reusability of the code.

The general method form in java is as follows:

修饰符 返回值类型 方法名(参数类型 参数名1, 参数类型 参数名2, ..., 参数类型 参数名n){
    方法语句;
    return 返回值;
}

The meaning of each element is as follows:

  • Modifiers: generally include public, protected, private, static, final, etc.
  • Return value type: the data type that the method needs to get the result at the end.
  • Method name: a name for the method to achieve this function, so that we can call it elsewhere.
  • Parameters: Parameters are divided into actual parameters and formal parameters. The actual parameters are the parameters that participate in the actual calculation of the method, and the formal parameters are the formal parameters used by the method definition to receive the actual parameters.
  • Parameter type: the data type of the parameter
  • Parameter name: the variable name of the parameter
  • Method statement: the code that completes the function in the method
  • return: The statement that ends the method.
  • Return value: The final result of the method completion function, which is returned to the caller through return.

We use code to test how to use the method:

public class Main {
    public static void main(String[] args){
        System.out.println(mysum(10,10));
    }

    public static int mysum(int a,int b){
        return a+b;
    }
}

The following is the result of the operation:

 

Second, the method of attention

  • The method is not called and not executed
  • Methods and methods are in a level relationship, and cannot be nested definitions
  • When the method is defined, the parameters are separated by commas
  • No need to pass the type of data when the method is called
  • If the method has a clear return value, you must write return + return value statement

 

Three, method overload

In the same class, the method name is the same, the parameter list is different, and the return value type can be the same or different. This is called method overloading.

The parameter list can be divided into:

  • The number of parameters is different
  • The data type of the parameter is different
  • The order of the parameters is different

We still use the above summation method as an example to understand method overloading:

public class Main {
    public static void main(String[] args){
        System.out.println(mysum(10,10));
        System.out.println(mysum(10,10,10));
        System.out.println(mysum(0.5,0.5));
    }
    public static int mysum(int a,int b){
        return a+b;
    }
    public static int mysum(int a,int b,int c){
        return a+b+c;
    }
    public static double mysum(double a,double b){
        return a+b;
    }
}

You can see that we have written three methods, the method name is mysum, the difference is their parameter list,

They are two int, three int and two double respectively, and then all three parameters are called in the main function. The following is the running result:

It can be seen that the program faces different parameters and uses the same parameter name to complete the realization of the function, which greatly enhances the fault tolerance of the code.

Guess you like

Origin blog.csdn.net/weixin_39478524/article/details/109484566