[Crazy God Talks about Java] java method

Method definition and call

Example: public static void arbitrary name

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

...

method body

...

return return value;

}

Method call:

If the method returns a value, the method call is often treated as a value, for example: int large=compare(30,40);

If the method return value is void, the method call must be a statement, for example: System.out.println("Hello,World");

java is passed by value

Passing by reference is passing a pointer, passing by value is to copy the value of the parameter

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

    }
    //Addition, the static method calls the non-static method, which is called through the class object, such as new Demo1().add(1, 2)
    public static int add(int a,int b){
        return a+b;
    }
    public static void main(String[] args) {
        test();
        //The main method should be kept as clean and tidy as possible, and the program can be called from the outside through the method, which is similar to functions in other languages
    }
    public static void test(){
    //...program content
    }
    public static void main(String[] args) {
        int large=compare(10,20);
        System.out.println(large);

    }
    //than size
    public static int compare(int num1,int num2){
        int result=0;
        if (num1==num2){
            System.out.println("num1=num2");
            return 0;//termination method
        }
        if (num1>num2){
            result=num1;
        }else{
            result=num2;
        }

        return result;
    }

method overloading

Overloading is a function in a class that has the same function name but different parameters.

rule:

  • method names must be the same

  • The parameter lists must be different (different number, or different types, or different order of parameter types, etc.)

  • The return types of the methods can be the same or different

  • Merely having a different return type is not enough to call a method overloading

public static double max(double num1,double num2){...}
public static int max(int num1,int num2){...}
public static int max(int num1,int num2,int num3){...}
//Methods can have unlimited multiple names, but make sure the parameter types are different

command line parameter

package method;

public class Demo03 {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]:"+args[i]);
        }
    }
}

If you run a class file directly, you need to find the path of its package, otherwise it cannot be executed. (The first line of execution ends to generate a class file, and javac compiles it)

variable parameter

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, any normal parameter must be declared before it.

A method without static modification is called an instance method, which can only be used after creating an object, such as new

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();//new+class, alt+enter auto-completion
        demo04.test(1,3,4,56,6);//The essence is an array, which is equivalent to a variable-length array
    }
    public void test(int x,int... i){
        System.out.println(i[0]);
        System.out.println(i[3]);
    }//The variable parameters are stored in the form of an array, but the length of the array is not known, so it is convenient to allocate memory at the back
}

recursion

Recursion means that method A calls method A and calls itself.

A recursive structure consists of two parts:

Recursive header: when not to call self method. If there is no head, it will fall into an infinite loop.

Recursive body: when do you need to call your own method

Do not use recursion for large calculations

    //5! 5*4*3*2*1
    public static void main(String[] args) {
        int result=f(5);
        System.out.println(result);

    }
    public static int f(int n){
        if(n==1){
            return 1;
        }else{
            return n*f(n-1);
        }

    }

Operation

Write a calculator that requires the addition, subtraction, multiplication, and division functions, and the ability to receive new data cyclically, through user interaction.

Ideas:

Write 4 methods: addition, subtraction, multiplication and division

Utilize loop + switch for user interaction

Pass the two numbers that need to be manipulated

output result

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter the first number");
        int a=scanner.nextInt();

        System.out.println("Please enter an operator");
        String op=scanner.next();

        System.out.println("Please enter a second number");
        int b=scanner.nextInt();

        switch (op){
            case "+":
                System.out.println(sum(a,b));
                break;
            case "-":
                System.out.println(sub(a,b));
                break;
            case "*":
                System.out.println(mul(a,b));
                break;
            case "/":
                System.out.println(div(a,b));
                break;
        }



    }
    public static int sum(int a,int b){
        return a+b;
    }
    public static int sub(int a,int b){
        return a-b;
    }
    public static int mul (int a,int b){
        return a*b;
    }
    public static int div(int a,int b){
        return a/b;
    }

Tags: Java

Posted by wildwobby on Thu, 26 Jan 2023 06:51:08 +1030