Java basic summary method, array


A Java method is a collection of statements that together perform a function; Method is an orderly combination of steps to solve a class of problems, which is contained in classes and objects; Methods are created in the program and referenced elsewhere

Method definition and call


Java method is similar to functions in other languages. It is a code fragment used to complete specific functions. Generally, defining a method includes the following syntax:

Method contains a method header and a method body.

Modifier: This is optional and is used to tell the compiler how to call the method; Define the access type of the method

Return value type: a method may return a value. Return ValueType is the data type of the method's return value; However, the operation performed by some methods does not need to return the value return. In this case, return ValueType is the keyword void (return 0; has the function of terminating the method)

Method name: is the actual name of the method

Parameter type: a parameter is like a placeholder. When a method is called, it passes a value to the parameter, which is called an argument or variable

Formal parameter: used to receive external input data (used to define parameters) when the method is called

Argument: the data that the calling parameter actually passes to the method

For example:

public class demo10 {
    public static void main(String[] args) {
        //Actual parameters: the parameters passed to him by the actual call
        int sum =add(12,12);

       //Formal parameters: used to define
    public static  int add(int a,int b){
        return a+b;


Method body: the method body contains specific statements that define the function of the method

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


Method body


Return return value



When a method returns a value, the method call is usually treated as a value

int   test = add(30,40); 

When the return value is void, the method call must be a statement


Method overloading

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

Rules for method overloading:

1. Method names must be the same

2. The parameter list must be different (different number, different type, different parameter order)

3. The return types of methods can be the same or different

4. Just different return types are not enough to overload methods

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

For example:

public class Demo10 {
    public static void main(String[] args) {
      Demo10 demo10 =new Demo10();

    public void  test(int...i){



Call method A recursively

Recursive structure:

Recursive header: when not to call its own method. If there is no header, it will fall into an endless loop

Recursive body: when to call its own method

Overview of arrays

An array is an ordered collection of data of the same type; Array describes several data of the same type, which are arranged and combined in a certain order; Each data is called an array element, and each array element can access them through a subscript; Array elements are accessed by index. The array index starts from 0

Array declaration creation

You must first declare this array variable before you can use the array in your program

Syntax format:

dataType [] arrayRefVar;


dataType  arrayRefVar [];

The new operation is used in the Java language to create arrays

dataType [] arrayRefVar = new dataType [arraySize];

Get array length:


Four basic features of arrays:

1. Its length is determined. Once the array is created, its size cannot be changed

2. Its elements must be of the same type, and mixed types are not allowed

3. The elements in the array can be any data type, including basic data type and reference type

4. Array variables are reference types. Arrays can also be regarded as objects. Each element in the array is equivalent to the member variables of the object. The array itself is an object. In Java, the object is in the heap, so the array object itself is in the heap whether it saves the original type or other object types

Array usage

public class demo11 {
    public static void main(String[] args) {
        int [] nums ={1,2,3,4,5,6,7,8,9,10};
        //Print all array elements
        for (int i = 0; i < nums.length; i++) {
        //Calculate the sum of array elements
            int  sum  =0;
            for (int i1 = 0; i1 < nums.length; i1++) {
                sum +=nums[i1];

        System.out.println("The sum of elements is:"+sum);
        //Find the largest array element
        int max =nums[0];
        for (int i = 0; i <nums.length ; i++) {
            if (nums[i]>max){
        System.out.println("The maximum value of the element is:"+max);
        //For each loop can directly output all array elements, but the subscript cannot be obtained
        for (int num : nums) {


Multidimensional array

Multidimensional array can be regarded as an array of arrays;

Columns such as two-dimensional array: int # a[] [] =new int [2] [5]; (this array can be regarded as an array with two rows and five columns)

Arrays class

Array tool class java util. Arrays  

There are no methods for us to call array objects, but the API provides a tool class Arrays for us to use, so we can perform some basic operations on data; The methods in the Arrays class are static methods decorated with static. When using them, you can call them directly with the class name instead of using objects

Common functions:

1. Assign a value to the array: through the fill method

2. Sort the array: sort the array in ascending order through the sort method

3. Compare arrays: use the equals method to compare whether the element values in the array are equal

4. Find array elements: binary search can be performed on the sorted array through the binarySearch method

Keywords: Java Back-end

Added by poppy28 on Thu, 17 Feb 2022 03:25:34 +0200