Introduction to Java - definition, call and overload of methods

Keywords: Java Back-end

1. Format of definition method

Modifier return value type method name(parameter list){ 
	//Code omission 
	return result; 
}
  • Modifier: public static fixed writing method
  • Return value type: the data type representing the result of the method operation. After the method is executed, the result is returned to the caller
  • Parameter list: unknown data of the method during operation, which is passed when the caller calls the method
  • Return: bring the result of the method execution to the caller. After the method is executed to return, the overall method operation ends

2. Flow diagram of calling method

3. Definition method exercise

  1. Exercise one
    Compare whether two integers are the same
  • Analysis: to define a method to implement a function, you need to have two explicit, namely, the return value and the parameter list.
    ∘ \circ ∘ clear return value: when comparing integers, there are only two possible comparison results, the same or different. Therefore, the result is Boolean, and the same comparison result is true.
    ∘ \circ ∘ explicit parameter list: the two integers compared are uncertain, so two int type parameters are defined by default.
public class Method_Demo3 { 
	public static void main(String[] args) { 
		//Call the compare method and pass two integers 
		//And receive the result calculated by the method, Boolean value 
		boolean bool = compare(3, 8); 
		System.out.println(bool); 
	}
	/* 
		Defines a method to compare whether two integers are the same 
		Return value type, boolean type of comparison result 
		Parameter: two integers involved in the comparison are uncertain 
	*/
	public static boolean compare(int a, int b) { 
		if (a == b) { 
			return true; 
		} else { 
		return false; 
		} 
	} 	
}
  1. Exercise 2
    Calculate the sum of 1 + 2 + 3... + 100
  • Analysis: to define a method to implement a function, you need to have two explicit: return value and parameter.
    ∘ \circ ∘ specify the return value: the sum of 1 ~ 100 must be an integer after calculation, and the return value type is int
    ∘ \circ ∘ explicit parameters: the calculated data is known in the requirements, there is no unknown data, and the parameters are not defined
public class Method_Demo4 { 
	public static void main(String[] args) { 
		//Call method getSum 
		//And receive the result calculated by the method, integer 
		int sum = getSum(); 
		System.out.println(sum); 
	}
	/* 
		Define the summation method for calculating 1 ~ 100 
		Return value type, calculation result integer int 
		Parameter: no uncertain data 
	*/
	public static int getSum() {
		//Define variable save summation 
		int sum = 0; 
		//Cycle from 1 to 100 
	for (int i = 1; i <= 100; i++) { 
			sum = sum + i; 
		}
	return sum; 
	} 
}
  1. Exercise three
    Print indefinitely
  • Analysis: to define a method to implement a function, you need to have two explicit: return value and parameter.
    ∘ \circ ∘ clear return value: just print HelloWorld in the method. There is no calculation result, and the return value type is void.
    ∘ \circ ∘ clear parameter: if it is unclear after printing several times, the parameter defines an integer parameter
public class Method_Demo5 { 
	public static void main(String[] args) { 
		//Call the method printHelloWorld to pass an integer 
		printHelloWorld(9); 
	}
	/*
		Define the HelloWorld method for printing 
		The return value type has no result void 
		Parameter: print several times indefinitely 
	*/
	public static void printHelloWorld(int n) { 
	for (int i = 0; i < n; i++) { 
			System.out.println("HelloWorld"); 
		} 
	} 
}

4. Considerations for defining methods

  • Define the location outside the method in the class.
  • The return value type must be the same as that returned by the return statement, otherwise the compilation fails.
  • You cannot write code after return. Return means that the method ends. All subsequent code will never be executed. It is invalid code.
public static int getSum(int a,int b) { 
	return a + b; 
	System.out.println("Hello");// Error. return has ended. It will not be executed here. Invalid code 
}

5. Three forms of calling methods

  1. Direct call: direct write method name call
public static void main(String[] args) { 
		print(); 
	}
	public static void print() { 
		System.out.println("Method called"); 
}
  1. Assignment call: call a method, define a variable in front of the method, and receive the return value of the method
public static void main(String[] args) { 
	int sum = getSum(5,6); 
		System.out.println(sum); 
	}
	public static int getSum(int a,int b) { 
		return a + b; 
}
  1. Output statement call:
  • Invoke the method in the output statement, System.out.println (method name ()).
public static void main(String[] args) { 
		System.out.println(getSum(5,6)); 
	}
	public static int getSum(int a,int b) { 
		return a + b; 
}
  • A method of type void cannot be called with an output statement. Because there is no result after the method is executed, nothing can be printed.
public static void main(String[] args) { 
		System.out.println(printHello());// Error, cannot call void type method with output statement 
	}
	public static void printHello() { 
		System.out.println("Hello"); 
}

6. Method overload

Method overloading: more than one method with the same name is allowed in the same class, as long as their parameter lists are different, regardless of modifiers and return value types.

  • Parameter list: different numbers, data types and orders.
  • Overloaded method call: the JVM calls different methods through the parameter list of the method.

7. Method overload exercise

  1. Exercise one
    Compare whether the two data are equal. The parameter types are two byte types, two short types, two int types and two long types, and are tested in the main method
public class Method_Demo6 { 
public static void main(String[] args) {
        //Define variables of different data types
        byte a = 10;
        byte b = 20;
        short c = 10;
        short d = 20;
        int e = 10;
        int f = 10;
        long g = 10;
        long h = 20; // call
        System.out.println(compare(a, b));
        System.out.println(compare(c, d));
        System.out.println(compare(e, f));
        System.out.println(compare(g, h));
    }

    // Two byte type
    public static boolean compare(byte a, byte b) {
        System.out.println("byte");
        return a == b;
    }

    // Two types of short
    public static boolean compare(short a, short b) {
        System.out.println("short");
        return a == b;
    }

    // Two int s
    public static boolean compare(int a, int b) {
        System.out.println("int");
        return a == b;
    }

    // Two long type
    public static boolean compare(long a, long b) {
        System.out.println("long");
        return a == b;
    }
}
  1. Exercise 2
    Simulate the effect of the println method in the output statement, output what type of data is passed, and only one method name println is allowed to be defined.
public class Method_Demo7 {
	public static void main(String[] args) {
	        myPrint((short)100); //short
	        myPrint(1001); // int
	        myPrint("Hello World!!"); // String
	    }
	    public static void myPrint(byte num){
	        System.out.println(num);
	    }
	    public static void myPrint(short num){
	        System.out.println(num);
	    }
	    public static void myPrint(int num){
	        System.out.println(num);
	    }
	    public static void myPrint(long num){
	        System.out.println(num);
	    }
	    public static void myPrint(float num){
	        System.out.println(num);
	    }
	    public static void myPrint(double num){
	        System.out.println(num);
	    }
	    public static void myPrint(char zifu){
	        System.out.println(zifu);
	    }
	    public static void myPrint(boolean is){
	        System.out.println(is);
	    }
	    public static void myPrint(String str){
	        System.out.println(str);
	    }
}

Posted by Chris1981 on Fri, 03 Dec 2021 21:59:28 -0800