Java basic study notes-5

Preface

The Java programming language is a high-level programming language widely used in the field of software development. Its powerful features and cross-platform nature make it the language of choice for many developers. This article will introduce some key concepts of Java programming, including function overloading, variable parameters, value transfer, recursion, etc. These concepts are the basis of Java programming and are essential for understanding and mastering the Java language.

Java basic study notes-1
Java basic study notes-2
Java basic study notes-3
Java basic study notes-4

1. Function Overloading

Function overloading is a common concept in Java programming that allows multiple methods to be defined in the same class with the same name but different parameter lists. The following is an example of function overloading:

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        f01(1.0);
        f01(1);
    }

    public static void f01(double money) {
    
    
        System.out.println("f01(double money) 被调用了");
    }

    public static int f01(int age) {
    
    
        System.out.println("f01(int age) 被调用了");
        return 100;
    }
}

Note that function overloading has nothing to do with return values, only parameter lists.

2. Variable parameters (Varargs)

Variadics are a powerful feature in Java that allow methods to accept a variable number of parameters. Here is an example of a variadic argument:

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        f01(new int[]{
    
    1,3,5,7,9});
        f01();
        f01(1);
        f01(1, 2);
        f01(1, 2, 3);
    }

    public static void f01(int... x) {
    
    
        System.out.println("f01(int...) 被调用了");
        System.out.println(Arrays.toString(x));
    }
}

Regardless of how many arguments are passed, variadic methods treat them as an array of integers.

3. Variable parameters and enhanced for loop

In Java, variadic parameters can be used in conjunction with enhanced for loops to handle a variable number of parameters. Here is an example:

public class Demo03 {
    
    
    public static void main(String[] args) {
    
    
        f01(new int[]{
    
    1,3,5,7,9});
        f01();
        f01(1);
        f01(1, 2);
        f01(1, 2, 3);
    }

    public static void f01(int... arr) {
    
    
        System.out.println("f01(int...) 被调用了");
        // 自动循环arr数组,也知道何时结束,每次取出一个送到变量a上
        for (int a : arr) {
    
     // foreach输出(增强版for循环),没有下标了
            System.out.println(a);
        }
    }
}

The above example demonstrates how to use variadic parameters to accept a variable number of integer parameters and use an enhanced for loop to iterate over these parameters. This makes it more convenient to handle an indefinite number of parameters.

4. Value transfer of basic data types

In Java, primitive data types are passed by value, which means that the method receives a copy of the original value rather than the original variable itself. Here is an example:

public class Demo04 {
    
    
    public static void main(String[] args) {
    
    
        int y = 10;
        f01(y); // y实参。都是值传递。
        System.out.println("Y:" + y);
    }

    /*
        x,形参
     */
    public static void f01(int x) {
    
    
        System.out.println(x);
        x++;
        System.out.println(x);
    }
}

In this example, f01the method receives ythe value of the variable and then operates on this value. However, this does not affect ythe value of the original variable, so when printed yit still outputs 10.

5. Array value transfer

Unlike primitive data types, arrays are also passed by value when passed to methods. However, the value of an array is a reference to the array, not the array's contents. This means that operations on the array within the method will affect the original array. Here is an example:

import java.util.Arrays;

public class Demo05 {
    
    
    public static void main(String[] args) {
    
    
        int[] y = {
    
    10, 20, 30};
        f01(y); // y实参。都是值传递。
        System.out.println("y: " + Arrays.toString(y));
    }

    /*
        x,形参
     */
    public static void f01(int[] x) {
    
    
        System.out.println(Arrays.toString(x));
        x[1]++;
        System.out.println(Arrays.toString(x));
    }
}

In this example, f01the method receives ya reference to an array of integers. When an array element is modified inside a method, the original array is also affected, so at the ytime of printing, the value of the second element has been modified.

6. List files recursively

In Java programming, recursion is a powerful technique for working with file and directory hierarchies. The following is a sample program that demonstrates how to recursively list all files in a specified directory and files in subdirectories:

import java.io.File;

public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        showFiles(new File("D:\\202203\\正科\\08_补充\\附件\\a"));
    }

    /*
        展示文件夹下所有的文件,包含子文件夹下的文件
     */
    public static void showFiles(File file) {
    
    
        File[] files = file.listFiles(); // 枚举该文件夹file对象下的文件或文件夹对象
        for (File f : files) {
    
    
            if (f.isFile()) System.out.println(f.getName()); // 文件
            if (f.isDirectory()) showFiles(f); // 递归调用文件夹
        }
    }
}

In this example, we use a recursive method showFilesto traverse all files and subdirectories in the specified directory. Specific steps are as follows:

  1. showFilesThe method accepts Fileas parameter an object representing the directory in which the files are to be listed.
  2. Use listFilesmethod to get all files and subdirectories under a directory and store them in filesan array.
  3. Use an enhanced for loop to iterate through fileseach file or directory in the array.
  4. For each element, we check if it is a file ( f.isFile()). If it's a file, we print the file name.
  5. If the element is a directory ( f.isDirectory()), the method is called recursively showFilesto continue traversing the contents of the directory.

This example shows how to use recursion to process file and directory structures and is one of the common use cases for handling file system operations.

7. Recursive summation

Recursion is a programming technique where a function calls itself. Here is a recursive example:

public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(sum(100));
    }

    public static int sum(int count) {
    
    
        if (count == 1) return 1; // 结束条件
        return sum(count-1) + count; // 递归表达式
    }
}

This example demonstrates a recursive function sum that recurses from 1 to a given positive integer count and calculates the sum. The end condition of the recursion is that count is equal to 1, and then the sum is calculated through the recursive expression sum(count-1) + count.

Recursion is a powerful programming technique that can be used to solve many problems, but the end condition of the recursion needs to be handled carefully to avoid infinite recursion.

Summarize

This article introduces several important concepts in Java programming in detail, including function overloading, variable parameters, value transfer, and recursion. These concepts are the foundation of Java programming and are essential for writing efficient and flexible Java code. By studying the examples and explanations in this article, readers can better grasp these concepts and improve their Java programming skills. Both beginners and experienced developers can gain valuable knowledge from this article to help them succeed in the field of Java programming. In future projects, these concepts will become important tools for solving complex problems and developing powerful applications.

Guess you like

Origin blog.csdn.net/qq_42531954/article/details/132722675
Recommended