Java method learning

Java Procedure

What is method

A method is a collection of statements that are grouped together to perform a function.

Method naming rules, initial lowercase + hump rule.

Method is contained in a class or object.

Methods are created in the program and referenced elsewhere.

Design method principle: a method only completes one function, maintains the atomicity of the method, and is conducive to later expansion.

Method definition and call

Modifier return value type method name (parameter type parameter name){

...

Method body

...

Return return value

}

Modifiers: public, static, final, private Optional

Return value type: void is used if there is no return value. If there is a return value, use return.

Parameter type: optional. Argument: the parameter actually passed when calling this method. Formal parameter: a parameter that is compiled randomly when defining a method.

 public static void main(String[] args) {
        int sum=add(1,2);
        System.out.println(sum);
    }
    public static int add(int a,int b){
        return a+b;
    }
}

a. B is a formal parameter and 1 and 2 are arguments.

Method body: the specific statement contained in the method, which defines the function of the method.

About return

The output of the code I wrote is empty and there is nothing.

public static void main(String[] args) {
    max(1,2);

}
public static int max(int a ,int b){
    if(a>b){
       return a;
    }else if(a<b){
        return b;
    }else {
        System.out.println("a and b The same big as that");
        return 0;
    }

Correct code:

public static void main(String[] args) {
    int m=max(1,2);
    System.out.println(m);

}
public static int max(int a ,int b){
    int result=-1;
    if(a>b){
      result= a;
    }else if(a<b){
        result= b;
    }else {
        System.out.println("a and b The same big as that");
        return 0;
    }
    return result;
}

Return means to terminate the program in addition to the return value;

Method supports two calling methods: select according to whether the method has a return value:

When a method returns a value, the method call is usually regarded as a value, such as:

int m=max(10,20);

system.out.println(m);

If the return value of the method is void, the method call must be a statement.

system.out.println("hello,IGOT7");

Calling method: object name Method name (argument list)

Value Passing: Java is value passing. [copy] value transfer is for basic variables. It transfers a copy of the variable. Changing the copy does not affect the original variable.

Reference passing: reference passing is generally for object-oriented variables. It passes a copy of the address of the object, not the original object itself.

Method overloading

Overloading is a function with the same function name but different formal parameters in a class.

Overload rules:

Method name must be the same; The parameter list must be different (different number, or different type, and different parameter order); The return value types of methods can be the same or different; Just because the return type is different is not enough to become a method overload.

Command line parameters

If you want to pass messages when running a program, you need to pass the command line parameters to the main () function.

Pass parameters on the command line.

Variable parameter

Java supports passing variable parameters of the same type to a method.

In the method declaration, add an ellipsis after specifying the parameter type

Only one variable parameter can be specified in a method. It must be the last parameter of the method, and any ordinary parameter must be declared before it.

public static void main(String[] args) {
    Demo04 d=new Demo04();
    d.test(1);

}
public void test(int ...i){
    System.out.println(i[0]);
}

recursion

Method A calls method A, which is recursion. Is to define an infinite set of objects with finite statements.

Recursion includes:

Recursive header: defines when there is a method that does not call itself, and no header will enter an endless loop.

Recursive body: when to call its own method.

public static void main(String[] args) {
    System.out.println(f(3));
}
public static  int f(int n){
    if(n==1){
        return 1;
    }else {
        return n*f(n-1);
    }
}

Implement a simple calculator:

It is required to practice loops, switch es and method calls.

public static void main(String[] args) {
    Scanner scanner=new Scanner(System.in);
    double a=scanner.nextDouble();
    String op=scanner.next();
    double b=scanner.nextDouble();
    double r=0;
   while(scanner.hasNext()){
        switch (op){
            case "+":
                r=add(a,b);

                break;
            case "-":
                r=subtract(a,b);

                break;
            case "*":
                r=multiply(a,b);

                break;
            case "/":
                r=divide(a,b);

                break;
        }
       System.out.println(r);
    }
}

public static double add(double a,double b){
    return a+b;
}
public static double divide(double a,double b){
    return a/b;
}
public static double subtract(double a,double b){
    return a-b;
}
public static double multiply(double a,double b){
    return a*b;
}

Error: continuous input processing is not implemented.

Posted by starsol on Wed, 11 May 2022 13:51:06 +0300