Zero-based Java Learning Tutorial - Four: Methods

Keywords: Java JavaSE

What is the method

Methods in Java are similar to functions in the c language
At Syem.out.println();in
System is a class, out is an object, println() is a method

  1. Method is a collection of statements that together perform a function
  2. Methods are contained in classes and objects
  3. Methods are created in programs and referenced elsewhere
  4. Naming of methods, hump rules

Definition and use of methods

Definition of method

Format:

Modifier Return Value Type Method Name (Parameter Type Parameter Name) {
...
Method Body
...
Return return value;
}

Modifier

(Optional) Tell the compiler how to call this method

return type

That is, the type of value the method ultimately returns, and no return value is to make void

Method Name

The actual name of the method, which together with the parameter list forms the method signature

Parameter type

A parameter is a placeholder, and when a method is called, a value is passed to the parameter, which is called an argument or a parameter. A list of parameters refers to the type, order, and number of parameters of the method.

Formal parameters

A formal parameter that receives data from outside sources when a method is called

Actual parameters

That is, the actual data passed to the method when it is called

Method Body

Statements that specifically define the function of this method

public class closeDown {
    //main method
    public static void main(String[] args) {
        int a=1,b=2;
        //Call Method
        int sum =add(1,2);//Here 1, 2 is the argument
        System.out.println(sum);
    }
    //The addition method, public,static, is a modifier and cannot be used in main without static, where a and b are formal parameters
    public static int add(int a,int b){
        return a+b;
        }
}

Use of methods

Call Format

Method with Return Value
  1. Object name. Method name (argument list)
  2. For methods created in classes: first create a new variable with the class name, then use the variable name. Method name (list of arguments)
public class s01 {
    public static void main(String[] args) {
        s01 s=new s01();//Create a new variable with the class name
        s01.test(1,2);//Call Method
    }
    public static int test(int a, int b){
        return (a>b)?a:b;
    }
}
Method with no return value

The call method must be a statement
For example: System.out.println();

In addition, you can learn about value passing (Java) and reference passing on your own

Overload

That is, in a class, there are functions with the same name but different parameters

rule

  1. Method Name Same
  2. Parameter lists are different (number, type, order)
  3. Return types can be equal or unequal
  4. Simply returning different types is not enough to be an overload of a method

principle

When the method names are the same, the compiler matches one by one based on whether the parameter lists are the same or not, and if the matching fails, an error will be reported

Example

It's easy to write a method to compare two integer sizes, but it's limited to integers. Method overload allows you to compare multiple data types. Here we compare integers with mice.

public class s01 {
    public static void main(String[] args) {
         int a=10,b=20;
         float c=13.5f,d=3.14f;![Insert a picture description here](https://img-blog.csdnimg.cn/76e29f9dd6084e3ab1fbabc724f45037.png#pic_center)

        System.out.println(max(a,b));
        System.out.println(max(c,d));
    }
    public static int max(int a,int b)
    {
        return (a>b)?a:b;
    }
    public static float max(float a,float b)
    {
        return (a>b)?a:b;
    }
}

Command line arguments

Sometimes you want to pass a message to a program when it runs, depending on the command line arguments passed to the main() function
You can click on the terminal in the lower left corner of idea

At this point its default path is practice's path, where we can also manipulate the command controller

If we find the directory where the Java file is located, compile the file using the javac command and run it using the Java command, we will find that it cannot run. At this time, we will go back to the src directory and run the program to demonstrate the operation of the java file with the following code

public class Demo03 {
    public static void main(String[] args) {
     for(int i=0;i< args.length;i++)//args.length is the number of elements in the array
     {
         System.out.println("args["+i+"]:"+args[i]);//This means to output the element corresponding to this subscript
     }
    }//It doesn't matter if we don't know here. We'll learn arrays after rice
}


Variable parameters

Also known as infinite parameter, an ellipsis (...) is added to the method declaration after specifying the parameter type.

public class s01 {
    public static void main(String[] args) {
   s01 s=new s01();
   s.test(1,2,3,4,5);
    }
    //How many values are passed in when a method is called, and how many values are received by this array
    public static void test(int... i){
        System.out.println(i[0]);//Equivalent to the first element of a printed array
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
        System.out.println(i[4]);
    }
}

Note that there can only be one variable parameter in a method and that it must be the last parameter of the method

Posted by roughie on Sat, 09 Oct 2021 10:05:41 -0700