I am learning Java in VScode (Java method method)

My personal blog homepage: If "" can really escape 1️⃣ say 1️⃣ blog homepage
About Java basic grammar learning ----> You can refer to my blog: "I learn Java in VScode"
about Java array learning, JVM The heap and stack —> You can refer to my article I learn Java in VScode (Java one-dimensional array, two-dimensional array, heap and stack in JVM) remake

noun:

"Group 1
  • Actual parameter (actual parameter): The value actually passed to the method in the method call, also known as the actual parameter.
  • Ambiguous invocation: When there are multiple methods with the same name and parameter types, but the return types are different or cannot be matched exactly, the compiler cannot determine which method to call, resulting in ambiguous invocation.
  • Divide and conquer: A method of problem solving that divides a problem into smaller subproblems and solves them step by step, and then combines the solutions to obtain a solution to the original problem.
  • Formal parameter (formal parameter): The parameter declared in the method definition, also known as formal parameter. They are part of the method signature and are used to receive the values ​​of the actual parameters passed in the method call.
  • Information hiding: An object-oriented programming principle that hides the internal state and implementation details of an object through encapsulation, and only exposes the necessary public interfaces to improve code maintainability and security.
  • method: In object-oriented programming, a method is a block of code within a class or object that performs a specific task. It is used to encapsulate operations and functionality and can be reused by other code.
  • method abstraction: A concept that separates a method from its concrete implementation. It emphasizes focusing on the purpose, functionality, and behavior of a method, rather than specific implementation details.
  • Method overloading: Defining multiple methods in the same class with the same name but different parameter lists. Through method overloading, you can choose which method to call according to the parameters passed to the method, so as to provide more flexible method calls.
"2 teams
  • Method signature (method signature): The unique identifier of the method, including the method name and parameter list. Method signatures are used to distinguish different methods.
  • Modifier: In Java, modifiers are used to modify the behavior and properties of classes, methods, variables, etc. For example, public, private, staticetc. are modifiers.
  • Pass-by-value: A parameter passing method in which the value of the actual parameter is copied to the formal parameter when the method is called, and the method uses a copy of the formal parameter instead of a reference to the original actual parameter.
  • Parameter (parameter): A placeholder in the method definition that is used to receive the value passed to the method. Parameters are used to provide input to a method and are processed inside the method.
  • Return value type (return type): The data type specified in the method definition, indicating the type of result returned by the method. For example, int, double, Stringand so on can all be used as return value types.
  • Return value (return value): The value returned after the method execution is completed. According to the return type defined by the method, the method can returnreturn the corresponding value by using the statement.
  • Scope of variable : The visibility and access scope of the variable. The scope of a variable determines where the variable can be accessed and used.
  • Method to be perfected (stub): a simple, empty or incompletely implemented method, used as a placeholder in software development, and its specific implementation is to be improved later. It is usually used in scenarios such as testing and framework construction.

What is a method signature (method signature):

方法签名是指方法的唯一标识,它由方法的名称和参数列表组成。
方法签名用于区分不同的方法,并且在Java中是必须唯一的。

The method signature includes the following aspects:

  • Method name: The name of the method is used to identify the operation or behavior of the method.
  • Parameter (parameter) list: The parameter list contains the type, order and number of input parameters accepted by the method.

Method overloading can be achieved by using method signatures, that is, multiple methods with the same name but different parameter lists can be defined in the same class. By comparing method signatures, the compiler can determine which method to call.

Here is the format of an example method signature:
返回类型 方法名(参数类型1 参数名1, 参数类型2 参数名2, ...)

**Actual parameters must match the parameters defined in the method signature in order and quantity, and be compatible in type.

类型兼容是指不需要经过显式的类型转换,实参的值就可以传递给形参
例如,将int型的实参值传递给double型形参。**:

The actual parameters provided when the method is invoked (that is, the value passed to the method) must match the parameters defined in the method signature in order and number, and they must also be compatible in type.

Matching in order and quantity means that the order and quantity of the actual parameters (actual parameters) must be consistent with the order and quantity of the parameter list in the method definition when the method is called. If the order or number of parameters do not match, the compiler will report an error.

Type compatibility means that the type of the actual parameter passed to the method should be compatible with the type of the parameter defined in the method signature. If the type of the actual parameter does not match the type of the formal parameter, the compiler will usually attempt an implicit type conversion to make them compatible. For example, an actual parameter value of type int can be passed [pass-by-value] to a formal parameter of type double, because the double type can accommodate a wider range of values, so no explicit type conversion is required.

However, if the type of the actual parameter is not compatible with the type of the formal parameter through implicit type conversion, then the compiler will report an error. In this case, an explicit type conversion is required to convert the type of the actual parameter to the type required by the formal parameter to make them compatible.

To sum up, the warning means that when the method is called, the actual parameters must match the parameters defined in the method signature in order and number, and they must also be compatible in type, unless an explicit type conversion is required.

Java method method

Parameter List: The parameter list is the input to the method. Each parameter should have a type and a name. The parameter list is enclosed in parentheses, and the parameters are separated by commas.

Return Type: The return type is the output of the method. If the method doesn't return anything, the return type should be void. Otherwise, the return type shall be the type of the value returned by the method.

Method Body: The method body is the actual code of the method. It should be enclosed in curly braces.

Method —> a reusable block of code that encapsulates specific functionality

In Java, a method is a higher-level code organization that combines multiple statements and expressions into a single block of code and interacts with other code through input parameters and return values. . A method can be regarded as a functional module in a program, which consists of multiple statements and expressions, rather than the smallest execution unit. [A method can be regarded as a functional module in a program, which consists of multiple statements and expressions, rather than the smallest execution unit.

Java方法可以带有参数列表,并且可以在方法体中使用这些参数。
方法可以有返回值,返回值类型(return type)必须在方法声明时指定。如果方法没有返回值,则返回类型应该用void表示。
通过访问修饰符(public、private、protected等)来限制方法的访问权限。默认情况下,在同一个包中的其他类可以访问该方法。
Java方法可以被重载,这意味着你可以定义具有相同名称但不同参数列表的多个方法。根据传递给方法的参数类型和数量,Java编译器将自动确定要调用哪个方法。
在Java中,方法可以是静态的或实例化的。静态方法属于类本身,而不是类的任何特定实例。因此,它们不能访问非静态成员变量或非静态方法。实例化方法属于类的实例,它们可以访问实例变量和其他实例化方法。
Java中的方法可以递归调用,即方法调用自身。递归通常用于解决可以分解为更小子问题的问题,例如计算斐波那契数列或树形数据结构的遍历等。

The method of Java plays the role of encapsulating reusable code, which greatly improves the efficiency of the code.Reusability and maintainability. In Java, methods can be called by other classes and methods, which allows programmers to encapsulate commonly used functions into methods for multiple use in the program.
Using methods also helps to improve the readability and maintainability of the program, because the code blocks of the method can be written, tested and maintained independently.
In addition, Java methods can also receive parameters and return values, which enables the method to perform different operations and return different results according to different input parameters, thereby improving the flexibility and scalability of the program.

public int add(int x, int y) {
    
    
    return x + y;
}
int result = add(3, 5);

In Java, methods are usually defined in classes and can be called by other classes and methods. The definition of a method usually includes a method name, a list of input parameters, a return type, and a method body. Method invocation can be achieved by using the method name and passing parameters.

[1] The main method; the access control authority is public (public), is static, has no return value, can only use void, and a class can only have one main() method. [It cannot be run without the main method;]

(1) In Java, the main method is the entry point of the program. When you run a Java application, JVM looks for the main method and starts executing the program from there where public static void main(String[] args)
, public means the method can be accessed from anywhere, static means the method belongs to a class and not an object, void means the method does not return any value, main is the method name, and String[] args is an array of strings containing command-line arguments.

(2) In Java, the main method refers to the main method defined in a class. Every Java application must have a main method, otherwise the JVM will not be able to run the application. In a Java application, there can be multiple classes, but only one class can contain the main method.

[2] Method call

In Java, method invocation refers to the process of invoking a method in a program to perform a specific task. When the program executes to a method call statement, it jumps to the definition of the method and executes the code in the method. When the method completes, the program returns to the method call statement and continues with the next statement.
methodName(argument1, argument2, ..., argumentN);
methodName is the name of the method to call, argument1, argument2, ..., argumentN are the arguments passed to the method. If the method does not require parameters, nothing needs to be enclosed in parentheses.
In Java, a method can return a value or nothing. If the method returns a value, the return type must be specified in the method definition. The return type can be any Java data type such as int, double, String, etc. If the method does not return any value, the return type must be void.

public class MyClass {
    
    
  public static void main(String[] args) {
    
    
    int result = add(5, 10);
    System.out.println(result);
  }

  public static int add(int a, int b) {
    
    
    int sum = a + b;
    return sum;
  }
}

We define a method called addNumbers that takes two integer parameters and returns their sum. In the main method, we call the addNumbers method and store its return value in the result variable, which is then printed to the console.

[3] The format of the method

Method name: The name of the method should clearly describe what the method does. Method names usually start with a lowercase letter and use camelCase.

Parameter List: Parameters are the inputs accepted by the method. A parameter list is enclosed in parentheses and contains zero or more parameters. Each parameter consists of its type and name, separated by spaces. Parameters are separated by commas.

Return Type: A method can return a value. The return type specifies the type of return value. If the method does not return any value, the return type is void.

Method Body: The method body contains the actual code of the method. The method body is enclosed by a pair of curly braces.

public int add(int a, int b) {
    
    
    int sum = a + b;
    return sum;
}
//在这个例子中,方法名称是 add,参数列表是 (int a, int b),返回类型是 int,方法体是包含了两个整数相加的代码块。

[ 0 ]Why is the format of the main() method in Java fixed?

Java中main()方法的格式被固定为`public static void main(String[] args)`是因为这是Java虚拟机(JVM)在执行Java程序时的入口方法约定。

The specific reasons are as follows:

  1. The method name must be "main": When the JVM executes a Java program, it will look for a method named "main" from the specified class as the entry point of the program. Without a method named "main", the JVM cannot start the program.

  2. The return value type is void: the return value type of the main() method must be void, that is, no result is returned. This is because the JVM does not pay attention to the return result after executing the main() method, but determines the execution flow of the program according to other code logic in the program.

  3. The parameter list is a String array: there is only one parameter in the parameter list of the main() method, which is the String array. This parameter is used to receive the parameters passed to the program by the command line. For example, at runtime java MyProgram arg1 arg2, arg1 and arg2 will be passed as elements of the String array to the parameter args of the main() method.

  4. The method modifiers are public and static: since the main() method is the entry method of the program and needs to be called directly by the JVM, the public modifier must be used to make it visible to the outside world. In addition, when the JVM executes the main() method, it does not need to create an instance of the class, so the main() method must be declared as a static static method.

To sum up, the format of the main() method in Java is fixed public static void main(String[] args)to meet the requirements of the JVM on the entry point of the program and enable the program to be executed correctly. The unified regulation of this format is also convenient for developers to write and maintain Java programs.

[ 1 ] The definition and call format of ordinary methods ----> Note: The method must be defined first and then called, otherwise the program will report an error

//方法定义
public static void 方法名(){
    
    
方法体(就是打包起来的代码);}
//方法调用
方法名();

[2] Method definition and call with parameters

(1) Method definition and invocation with parameters have the following advantages:

Code reusability: Method definitions and calls with parameters allow you to write reusable code. You can write a method that takes parameters and does something. You can then call the method at multiple places in your program, passing it different parameters.

Code readability: Method definitions and calls with parameters can make your code easier to read and understand. By including parameters in a method definition, you can clearly express the purpose and expected input of the method. When calling the method, you can clearly see the value passed to the method.

Code flexibility: Method definitions and calls with parameters can make your code more flexible. By including parameters in a method definition, you can easily change the behavior of that method without changing the method's implementation. You can easily change the behavior of the method to suit different situations by passing different parameters when calling the method.

(2) Does the java return parameter not require a type?

According to the syntax of Java, the parameters of the method need to specify the type. This is to ensure that the compiler can check that the arguments are of the correct type at compile time. If you want to return a value from a method, you need to specify the type of the return value.

(3) Examples

public static void method(int num1,int num2){
    
    
int result = num1 + num2;
System.out.printIn(result);}

(4) format

//方法定义(单个参数)
public static void 方法名( 参数) ... ...

public static void method(int number){
    
     ...}
//多个参数
public static void 方法名( 参数1,参数2,.....{
    
    ....}

public static void getSum(int number1, intnumber2 ){
    
    。。。}
//方法使用(单个参数)
 方法名( 参数)method(变量);
(多个参数)
getSum(变量1,变量2)
Notice:

When the method is called, the number and type of parameters must correspond to the variables in the parentheses in the method definition, otherwise the program will report an error.

(5) Formal parameters and actual parameters

Formal parameter: the full name of the formal parameter (formal parameter), refers to the parameter in the method definition
Actual parameter: the full name of the actual parameter (actual parameter), the parameter in the method call

注意:方法调用时,形参和实参必须一一对应,否则程序将报错。

(6) Definition and call of method with return value

public static 返回值类型 方法名 (参数){
    
    
方法体;
return 返回值;
}
public static int getSum(int a, int b) {
    
    
int c=a+b;
return c;}

(7) Method of calling f with return value method

direct call: 方法名(实参);
assignment call: 整数类型 变量名 = 方法名 (实参);
output call:System.out.println(方法名 (实参));

[4] The complete definition format of the method

public static返回值类型方法名 (参数) [
方法体;
return 返回值:
}

[5] Precautions for the method

  1. If you don't call the method, it won't execute. So if you write a method but never call it, it will have no effect on the program.

  2. In Java, there is a horizontal relationship between methods and methods, and they cannot be defined nested in each other. If you need to call a method from another method, you can call that method from within that method.

  3. The order in which methods are written has nothing to do with the order in which they are executed. You can write methods anywhere in your program, and they can be called from anywhere in your program.

  4. The return type of the method (return type) is void, which means that the method has no return value. If your method doesn't need to return any value, you should set its return type to void. In this case, you can omit the return statement. If you're writing a return statement, you can't follow specific data.

  5. Below the return statement, you cannot write code, because it will never be executed, and it is an invalid code. If you wrote code below the return statement, it would never execute.

High-quality Java methods

Method names should be clear and describe exactly what the method does.
Methods should do only one thing, and should be as short and concise as possible. If a method is too long or complex, you should consider breaking it into smaller methods.
Methods should be well readable and maintainable. This means you should use meaningful variable names and comments so that others can easily understand your code.
Methods should have good error handling. This means that you should think about possible error conditions and write code to handle them.
method should have good properties. This means that you should avoid using excessive loops or recursion, and use efficient algorithms and data structures whenever possible.

other

Always use camelCase for variables, methods, and classes.
Always use curly braces inside a code block, even if the code block is only one line.
Always use the final keyword to declare immutable variables.
Always use a try-catch block to handle possible exceptions.
Always use the data structures and algorithms provided in the Java Standard Library for best performance and readability.

return keyword

1. In Java, the return keyword is used to end the method and return the result. If a method does not return a value, you can omit the return keyword. However, if a method has a return value, you must write the return keyword to indicate the end of the method and return the result.
2. It should be noted that if a method has a return value, but you do not use the return keyword in the method body to return the result, Java will return a default value by default.

Method overloading —> methods with the same method name and different parameters in the same class. The parameters that have nothing to do with the return value are different: the number is different, the type is different, and the order is different

Method overloading means that in the same class, you can define multiple methods with the same method name but different parameter lists. == The advantage of doing this is that it can improve the reusability and readability of the code.
1. In the same class, multiple methods with the same name are defined. These methods with the same name have the same function. 2.
Each method has a different parameter type or number of parameters. These methods with the same name constitute overloading. relation

[1] In Java, method overloading needs to meet the following conditions

The method name is the same, but
the parameter list 1 is different (parameter type, parameter number, parameter order are different)

public class Calculator {
    
    
    public int add(int a, int b) {
    
    
        return a + b;
    }

    public double add(double a, double b) {
    
    
        return a + b;
    }
}

Suppose we have a class Calculator, which defines two methods add, which accept two integers and two floating point numbers as parameters:

Calculator calculator = new Calculator();
int result1 = calculator.add(1, 2);
double result2 = calculator.add(1.0, 2.0);

It should be noted that method overloading is only related to the parameter list, and has nothing to do with the method's return value type (return type) and access modifier (modifier) ​​[(public, private, protected, etc.)]. Therefore, the following code is illegal:

public class Calculator {
    
    
    public int add(int a, int b) {
    
    
        return a + b;
    }

    // 编译错误:与上一个方法的参数列表相同
    public int add(int x, int y) {
    
    
        return x + y;
    }
}

【2】Precautions:

The Java virtual machine distinguishes methods with the same name by different parameters

public class MethodDemo {
    
    
public static int sum (int a, int b) {
    
    return a + b;
public static int sum (int a, int b, int c) {
    
    return a + b + c;}
sum(12a);
sum(12030);
}

In method overloading (method overloading), you need to pay attention to the following points:

The method name is the same, the parameter list is different, but the return value type (return type) and access modifier can be different.
Different parameter lists include different parameter types, parameter numbers, and parameter sequences.
When calling a method, the compiler will determine which method to call based on the type and number of parameters passed in.
If the parameter lists of the two methods differ only in the type of the return value, then the two methods cannot constitute overloading, which will cause a compilation error.

In addition, you need to pay attention to the following points:

Method overloading can only be done within the same class.
Method overloading cannot be distinguished solely by parameter names or parameter modifiers.
Method overloading cannot be distinguished only by the type of parameters, and the number and order of parameters also need to be considered.

在实际编程中,方法的重载可以提高代码的复用性和可读性,但是需要注意不要滥用方法的重载,否则会导致代码难以维护和理解。

The advantages of method overloading are:

Better readability: the method name is the same, but the parameter types and numbers are different, which will make the code clearer and easier to understand;
better flexibility: the same method name can be used multiple times, but different parameter types and numbers It can meet different needs;
it is easy to program: avoid writing many similar methods, and improve the reusability of code.

public (public) or static (static)

When we create an object of a class, we actually allocate a space in memory to store the object's properties and methods. Public methods are associated with an object instance, so they can only be called through the object. The advantage of this is that we can share the code of the method between different object instances, but each object instance can have its own property values.

In contrast, static methods are associated with the class itself, not with the object instance. Static methods can be called directly without creating an object of the class. This is because static methods already exist in memory when the class is loaded and do not require an object instance to access them.

public class MyClass {
    
    
    // 静态方法
    public static void myStaticMethod() {
    
    
        System.out.println("这是一个静态方法");
    }

//    // 公共方法
//    public void myPublicMethod() {
    
    
//        System.out.println("这是一个公共方法");
//    }

    public static void main(String[] args) {
    
    
        myStaticMethod(); // 可以直接调用静态方法

//        MyClass myObj = new MyClass(); // 创建一个对象
//        myObj.myPublicMethod(); // 通过对象调用公共方法
    }
}

The comparison yields:

// Static methods can be called directly without creating an object of the class, because static methods already exist in memory when the class is loaded, and no object instance is required to access them.

// In Java, when a class is loaded into memory, static methods are also loaded and stored in memory. This means that static methods can be called directly by the class name without creating an object of the class. This is because static methods are not tied to an instance of an object, but are associated with the class itself.

The presence of static methods has several advantages:

// Call directly through the class name: Since the static method is associated with the class itself rather than an object instance, you can call the static method directly through the class name without creating an object of the class.
// Shared code: Static methods can share code between different object instances. This means that multiple object instances can use the same static method without each object instance having its own copy of the method.
// No need to access object properties: Static methods cannot access object properties because they do not depend on the object instance. This makes static methods more independent and reliable, and can be executed without regard to the state of the object.
// However, it should be noted that static methods cannot access non-static member variables and non-static methods, because they can only access static member variables and static methods.

How the method is stored

In Java, methods are stored in class files. When you write Java code and compile it, the compiler produces a class file that contains the bytecodes for all the methods of the class. These bytecodes instruct the JVM how to execute methods at runtime.

If you want to see method bytecodes in class files, you can use a Java decompiler. For example, you can use the javap command to decompile a class file and view the methods within it.

CursorCode given by

The following is the method bytecode for an example class file:

public class Example {
    
    
  public static void main(String[] args) {
    
    
    System.out.println("Hello, world!");
  }

}
Decompiled bytecode:

public class Example {
    
    
  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Hello, world!
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return}

Primitive data types and reference data types

Basic data types:

##Integer type, floating point type, Boolean type, character type.

In Java, == primitive data types are stored by value. == This means that when you declare a variable of a basic data type, the computer will allocate a certain amount of memory space for the variable to store its value. These basic data types include: byte, short, int, long, float, double, char, and boolean.

Every primitive data type has a default value, and if you don't assign a value to the variable, it will be initialized to its default value. For example, the default value of the int type is 0, and the default value of the boolean type is false.

From a memory point of view

Values ​​of primitive data types are stored directly on the stack, not on the heap. This makes access to primitive data types faster because they don't need to be referenced to access their values.
In Java, these basic data types have corresponding wrapper classes, such as Byte, Short, Integer, Long, Float, Double, Boolean, and Character. These wrapper classes provide many useful methods, such as converting primitive data types to strings, and converting strings to primitive data types.

int myInt = 42;
double myDouble = 3.14159;
boolean myBoolean = true;
char myChar = 'a';

Reference data type: something new, address value

Reference data types include classes, interfaces, arrays, etc.
A variable of reference data type stores a reference to an object, not the object itself. This means that when we create an object, we actually allocate a space in heap memory and return a reference to that space. Therefore, a variable of the reference data type actually stores the address of the object in memory.

how to store

When we declare a reference type variable, Java will allocate a memory space for the variable to store the reference of the object. When we create an object, Java allocates a space for the object in the heap memory and returns a reference to the space. We can assign that reference to a variable so that the variable points to the object. When we use the variable, Java will find the location of the object in memory based on the reference stored in the variable and operate on it.

How to understand that the data value of the reference data type is stored in other spaces, and the address value is stored in its own space.

According to the provisions of Java, variables of basic data types directly store values, while variables of reference data types store the address values ​​of objects. This means that when we create an object, Java will allocate space for the object in the heap memory and return the address value of the object. We can then use that address value to access the object. Therefore, the data value of the reference data type is stored in the heap memory, and the variable itself just stores the address value of the object.

//以下是一个示例,说明如何创建一个对象并访问其属性:
public class Person {
    
    
    String name;
    int age;
}

public class Main {
    
    
    public static void main(String[] args) {
    
    
        // 创建一个Person对象
        Person person = new Person();
        person.name = "Alice";
        person.age = 25;

        // 访问该对象的属性
        System.out.println("Name: " + person.name);
        System.out.println("Age: " + person.age);
    }
}

In the example above, we created a Person object called "person" and set its name and age properties to "Alice" and 25. We then use the object's address value to access its properties. Therefore, the data value of the reference data type is stored in the heap memory, and the variable itself just stores the address value of the object.

From the point of view of memory—>data values ​​are stored in other spaces, and address values ​​are stored in the own space.

Values ​​of reference data types are stored on the heap, not on the stack. This means that accessing a value of a reference data type needs to be done by reference, not directly. Therefore, accessing reference data types is slower than accessing primitive data types.

String myString = "Hello World";
Object myObject = new Object();
int[] myArray = {
    
    1, 2, 3, 4, 5};

In this example, myString is a reference data type of type String, myObject is a reference data type of type Object, and myArray is an array of type int, which is also a reference data type.

The value transfer of the method -> the value transfer of the method from the perspective of memory: the parameter transfer in Java is the value transfer, that is, the modification of the parameters in the method will not affect the value of the original variable

(1) According to the rules of Java, all parameters are passed by value. This means that when a primitive or object type is passed as an argument to a method, what is actually passed is a copy of that value. So if you change the value of the parameter inside the method, the original value will not be changed. If the passed argument is an object, the copy and the original refer to the same object. So if you change the state of the object inside the method, the change will be reflected in the original object.

(2) Parameter passing in Java is value passing. From a memory perspective, when a method is called, a new stack frame is created for that method in stack memory. The stack frame contains information such as parameters, local variables, and return values ​​of the method. When a parameter is passed to a method, a copy of the value of the parameter is actually passed to the method, rather than passing the parameter itself to the method. Therefore, the modification of the parameters in the method will not affect the value of the original variable.

A few code examples:

GCD

public class PrimeNumbers {
    
    
    public static int gcd(int a, int b) {
    
    
        return b != 0 ? gcd(b, a % b) : a;
    }
}

LCM

public class PrimeNumbers {
    
    
    public static int gcd(int a, int b) {
    
    
        return b != 0 ? gcd(b, a % b) : a;
    }

    public static int lcm(int a, int b) {
    
    
        return a / gcd(a, b) * b;
    }

    public static void main(String[] args) {
    
    
        System.out.println(gcd(12, 123));
    }
}

Esperanto sieve:

import java.util.Scanner;

public class PrimeNumbers {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int max = 100000001;
        int[] arr = new int[max];
        int n = scanner.nextInt();
        int cnt = 0;

        for (int i = 1; i < max; i++) {
    
    
            arr[i] = 1;
        }

        for (int i = 2; i <= n; i++) {
    
    
            if (arr[i] == 1) {
    
    
                System.out.println(i);
                cnt++;
            }
            for (int j = i * i; j <= n; j += i) {
    
    
                arr[j] = 0;
            }
        }

        System.out.println(cnt);
    }
}

  1. A parameter list is a list of variables listed in a function or method definition. These variables are inputs to the function or method, and their values ​​can be used inside the function or method. Parameter lists are usually enclosed in parentheses, and variable names and types are separated by commas. ↩︎

Guess you like

Origin blog.csdn.net/m0_74154295/article/details/131195674