Java method definition / formal parameter / argument / return value / statement block / method overload

Keywords: JavaSE

Statement block

  1. Statement blocks (compound statements) are any number of simple Java statements enclosed in curly braces.
  2. Block determines the scope of the local variable. The program code in the block, as a whole, is to be executed together.
  3. A block can be nested within another block, but variables with the same name cannot be declared within two nested blocks.
  4. A statement block can use external variables, but external variables defined in the statement block cannot be used, because the scope of variables defined in the statement block is limited to the statement block.

Code example: defining variables in a statement block

// Test statement block
public class TestLan {
    public static void main(String[] args) {
        int n;
        int a;
        {
            int k; 
            int n; // Compilation error: variable n cannot be defined repeatedly
        }   // This ends the scope of variable k
    }
}

method

  1. A method is a piece of code used to complete a specific function, similar to functions in other languages.
  2. Method is used to define the behavior characteristics and function implementation of related instances of this class. Methods are abstractions of the behavioral characteristics of classes and objects. Methods are similar to procedure oriented functions.
  3. In process oriented, function is the most basic unit, and the whole program is composed of function calls.
  4. In object-oriented, the basic unit of the whole program is class, and the method is subordinate to class and object.

Method declaration format:

[Modifier 1 Modifier 2]   Return value type method name(Formal parameter list){
    Java Statement:
}

Method call method:

Object name. Method name (argument list)
Detailed description of the method:

  1. Formal parameter (written when defining a method): used to receive external incoming data when declaring a method.
  2. Argument (written when calling a method): the data actually passed to the method when calling the method.
  3. Return value: the data returned to the calling environment after the method is executed.
  4. Return value type: the data type of the return value agreed in advance. If there is no return value, it must be specified as void.

Code example: define method, call method

// Define method, call method
public class TestMethod {
    public static void main(String[] args) {
        // Define method object
        TestMethod tm = new TestMethod();
        // Call normal methods through objects
        tm.printSxt();
        // Call a parameter method with no return value through an object
        tm.add(30,40,50);
        // Call parameter method with return value through object: calculate number
        int c = tm.add1(10,20,30) + 100;
        System.out.println("Result of calculation:" + c);
    }

    // Add void: it means you don't need to return anything when printing. And there is no formal parameter defined when defining the method
    void printSxt(){
        System.out.println("Printed Sxt");
        System.out.println("Ah jun No one yet. November 1, 2021.");
    }

    // Parameter method with no return value
    void add(int a,int b,int c){
        int sum =  a + b + c;
        System.out.println("Calculation result of calling parameter method:" + sum);
    }

    // Parameter method with return value. Define the data type of the return value in front of the parameter method.
    int add1(int a,int b,int c){
        int sum = a + b + c;
        // Define the return value of the add1 method
        return sum;    // return function: 1. End the operation of the method. 2. return value
    }
}

Little knowledge

  1. Return: (1) ends the operation of the method. (2) Specify the return value (the return value will not be printed actively).
  2. Number, data type, and order of the arguments must match list of the formal parameters of the method declaration being invoked.
  3. When passing parameters in method calls in Java, follow the principle of value passing (the passed values are copies of data) (copies will assign a formal parameter to the corresponding method in the actual parameter value copy given by the calling method).
  4. The basic type passes the copy value of the data value.
  5. The reference type passes the copy value referenced by the object, but points to the same object.

Method overload

Overloaded methods are actually completely different methods, but their names are different.

Minefields: method overload

Composition conditions of method overload:

  1. Different meanings: parameter type, number and order are different.
  2. Only different return values do not constitute an overload of the method.
    For example, only the return value is different
int a(String str)(){} And void a(String str)(){}
  1. Only the name of the formal parameter is different, which does not constitute an overload of the method.
    For example, only the formal parameter names are different
int a(String str)(){} And int a(String s)(){}

Code example: method overloading

// Test method overload
public class TestOverload {
    public static void main(String[] args) {
        // Overloading different calling methods
        System.out.println(add(3,5));   // The result is 8
        System.out.println(add(3,4,5)); // The result is 12
        System.out.println(add(3.05,5));// The result is 8.05
        System.out.println(add(3,5.05));// The result is 8.05
        // Overloading of some common methods
        System.out.println();       // 0 parameters
        System.out.println(1);      // The argument is an int
        System.out.println(2.0);    // The parameter is a double (because float requires f/F to declare, it is a double)
        System.out.println("cc");   // Parameter is a string
    }
    // Summation method
    public static int add(int n1,int n2){
        int sum = n1 + n2;
        return sum;
    }
    // The same method name and different number of parameters constitute an overload
    public static int add(int n1,int n2,int n3){
        int sum = n1 + n2 + n3;
        return sum;
    }
    // The same method name and different parameter data types constitute an overload
    public static double add(double n1,int n2){
        double sum = n1 + n2;
        return sum;
    }
    // The same method name and different order of parameter definition types constitute an overload
    public static double add(int n1,double n2){
        double sum = n1 + n2;
        return sum;
    }
    /**
     * The same method name and different position order of parameters of the same type do not constitute an overload.
     * For example, int add(int n1,int n2) {} and int add(int n2,int n1) {}
     * Only the return value is different (that is, only the data type of the return value of the defined method is different), which does not constitute an overload of the method.
     * For example, int add(int n1,int n2) {} and double add(int n1,int n2) {}
    */
}

At this point, the knowledge points related to Java methods are over.

Posted by iii on Mon, 01 Nov 2021 03:23:58 -0700