Java code modularization - method

Posted by chomedey on Mon, 22 Nov 2021 14:44:19 +0100

Java code modularization - method

1. Concept of method

The purpose of Java method: modularity, reusability and simple application

2. Structure

3. Grammar

public satatic int add(int a,int b,int c){
int sum = a+b+c;
return sum;
  • public: access control character
  • Static: static / /!!!
  • int /double/void: enlarged return type
  • add: name of the method
  • int a,int b: parameter list
  • Braces: scope

4. Common grammatical errors are

  • The other set of methods cannot be used in the method
  • There is a return type in the method design, but there is no return in the method body!!! (note)
  • Scope - braces
  • Void does not need to be returned. If you use void, you need to print it in the method. If you use int/double, you need to print it in main

5. Enlarged formal parameters and arguments

Formal parameters are formal parameters

(int a ,int b); // a. B is the formal parameter of the method - formally, the two-point parameter can have any name
int x = add(4,6)//4 and 6 are the arguments of the method, a=4,b=6

6 method - overload

6.1 concept

If the same class contains two or more methods with the same method name but different number, order or type of method parameters, it is called method overloading or method overloading.

6.2 purpose of method overload

Reuse the name of the method in the class to avoid naming a large number of method names

For example:

 * Overload (solve the problem of the same name)
 * add_int
 * add_int_int
 * add_double_double
 * add_double_int
 * add_int_double
	public static int add(int a) {// Overload is only related to parameter type, quantity and order
		return a;

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

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

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

6.3 necessary conditions for method overload

  • In the same class
  • The method name is exactly the same
  • The parameter type or quantity of method is different

7 indefinite parameters

7.1 purpose of uncertain parameters

When a large number of overloaded methods affect the reading and use of the program.
The essence of no top parameter is to pass in an array, but the caller is allowed to pass in the number of parameters at will, which is more flexible

7.2 definition of indefinite parameters

Public void func (type... Parameter name){
//When the compiler runs, it converts the passed in arguments into arrays for execution

public static void main(String[] args) {
		func(1,2,4,2,3,4,121,213);//Usage: () can write any number of numbers

public static void func(int ... a  ){
   for(int i=0;i < a.length;i++){
       System.out.print(a[i] + "\t");

8 methods - common cases

8.1 define a method to determine whether the year is a leap year

public static boolean isLeap(int year){//The return value is boolean. Note!!!
    if(year % 400 == 0 || year % 4 == 0 && year % 100 != 0){
	   return true;
    return false;

8.1 define a method to determine whether n is a prime number

public static boolean isPrimer(int n){
    for(int i =2; i<=Math.sqrt(n);i++){//Math. Squart (n) means n under the root sign
        if(n % i ==0) return false;
    return true;

9 method - recursion

9.1 concept

recursion algorithm: in computer science, it refers to a method to solve the problem by repeatedly decomposing the problem into similar subproblems.

9.2 recursive execution process

  • 1 find out the stop condition of recursion
  • 2 find out the general term formula to solve recursion

9.3 recursive cases

Case 1 cumulative multiplication

public static int f(int n){
    if(n==1) return 1;  //Recursive termination condition
    return n * f(n-1); //General expression

Case 2 fibula and sequence

	 * Recursive fiboracci sequence 1 1 2 3 5 8 
	*Common practice
	public static void fib01(int n) {
		int n1 = 1;
		int n2 = 1;
		for(int i= 3;i<=n;i++) {//int i = 3, which means to start from the third item; I + +, not n + +!! see clearly
			int x = n1+n2;
	*Recursive approach
	public static int fib02(int n) {
		if (n == 1 || n == 2) {//The first and second terms of the fiboracci sequence are both 1
			return 1;
		return f(n - 1) + f(n - 2);

Topics: Linux Operation & Maintenance server