Methods and method overloading


1. [understanding] what is a method
2. [Master] method format
3. Implementation process of [understanding] method
4. Cases of [mastering] methods
5. [understanding] method overloading
6. [understanding] transfer of method parameters

1, What is the method?

method is a block of code that performs a specific function. Extract some duplicate code

  • Method is an ordered combination of steps to solve a class of problems
  • Method is contained in a class or object
  • Methods are created in the program and referenced elsewhere

be careful
Methods must be created before they can be used. This process becomes a method definition
After a method is created, it can not be run directly. It needs to be used manually before execution. This process becomes a method call

2, Defines the format of the method

Modifier return value type method name (parameter list){
//Code omission
return result;

Modifier: current fixed writing method of public static
Return value type: the data type representing the result of the method operation. After the method is executed, the result is returned to the caller
Parameter list: unknown data of the method during operation, which is passed when the caller calls the method
Return: bring the result of the method execution to the caller. After the method is executed to return, the overall method operation ends
Return result: the "result" here is called the return value of the method during development

Definition method considerations

  1. Definition location: definitions cannot be nested outside methods in a class.
  2. Method must be defined before use.
  3. void indicates that there is no return value. You can omit return or write return separately.
  4. You can't write code after return. Return means that the method ends. All subsequent codes will never be executed. It is invalid code.
  5. The parameters in () when defining a method are called formal parameters, and the parameters passed when calling the method are called actual parameters.

Three forms of calling methods

Direct call: direct write method name call
Assignment call: call a method, define a variable in front of the method, and receive the return value of the method
Output statement invocation: invoke method in output statement

matters needing attention
You cannot call a void method with no return value type with an output statement. Because there is no result after the method is executed, nothing can be printed

The code is as follows (example):

public class MethodDemo01 {
    public static void main(String[] args) {
        //Directly call the printHelloWorld method to print HelloWorld 10 times
        //Call sum method after assignment
        int add=sum(2,3);
        //The output statement calls the sum method

    public static void printHelloWorld() {       
        for (int i = 0; i < 10; i++) {
     public static int sum(int a,int b){
        int c;
        return c;


  • demand

Definition method implementation: calculate the sum of 1 + 2 + 3... + 100

  • analysis

    • Explicit parameter list

    The calculated data is known in the requirements, there is no unknown data, and no parameters are defined

    • Explicit return value

    The sum of 1 ~ 100 must be an integer after calculation, and the return value type is int

The code is as follows (example):

 public static void main(String[] args) {        
        //Call method getSum
        //And receive the result calculated by the method, integer
        int sum = getSum();

        Define the summation method for calculating 1 ~ 100
        Return value type, calculation result integer int
        Parameter: no uncertain data
    public static int getSum() {
        //Define variable save summation
        int sum = 0;
        //Cycle from 1 to 100
        for (int i = 1; i <= 100; i++) {           
            sum = sum + i;
        return sum;

3, Method overloading

In the same class, with the same method name and different parameter lists (different number and types of parameters), it has nothing to do with the return value

Parameter list: different numbers, data types and orders.

Overloaded method call: the JVM calls different methods through the parameter list of the method.

  • Overloading only corresponds to the definition of the method and has nothing to do with the method call. The call method refers to the standard format
  • Overloads only identify the names and parameters of methods in the same class, regardless of the return value. In other words, it is not possible to determine whether two methods constitute overloads with each other through the return value

The code is as follows (example):

public static void main(String[] args) {   
        System.out.println("1 To the sum of one hundred"+sum());
    public static int sum(int a,int b){
        int c;
        return c;
    public static void sum(){
        int a=0;
        for(int i=1 ; i<=100 ; i++){

Tags: Java JavaSE

Posted on Thu, 14 Oct 2021 22:05:23 -0400 by prometheuzz