Overview and usage of methods in Java

Java Procedure

Overview of methods

Java methods are collections of statements that together perform a function

  1. Method is an ordered set of steps to solve a class of problems
  2. Method is contained in a class or object
  3. Methods are created in the program and referenced elsewhere
  4. Methods can have parameters or no parameters

Advantages of the method

  1. Improve code reusability
  2. Improve program development efficiency
  3. It is conducive to program maintenance
  4. The program becomes shorter

Naming rules for methods

The first word of a method's name should start with a lowercase letter, and the subsequent word should start with a capital letter, such as testMethod

Definition of method

Modifier return value type method name (parameter type parameter name){
Method body
Return return value;
Method contains a method header and a method body. The following is a detailed explanation of all parts of a method:

  1. Modifier: modifier, which is optional, tells the compiler how to call the method and defines the access type of the method
  2. Return value type: a method may have a return value. returnValueType is the data type of the return value of the method. If some methods do not have a return value, the keyword of the return value type in this case is void
  3. Method name: is the actual name of the method. The method name and parameter table together constitute the method signature
  4. Parameter type: a parameter is like a placeholder. When a method is called, it passes a value to the parameter. This value is called an argument or variable. The parameter list refers to the parameter type, order and number of parameters of the method. The parameter is optional, and the method may not contain any parameters.
  5. Method body: the method body contains specific statements that define the function of the method
    Example 1
    The following example demonstrates how to define a method and call it:
 package com.tedu.cn;
//This class is used to test methods
public class Test_Method {
    //Main method
    public static void main(String[] args) {
        int result =  max(2,3);


    // Returns the larger value of two integer variables
    public static int max(int n1,int n2){
        int result =0;
        if (n1>n2){
            result =n1;
        return result;

Compilation results

Note: This program contains the main method and the max method. The main method is called by the JVM. In addition, the main method is no different from other methods.
The header of the main method is unchanged. As shown in the example, it takes the modifiers public and static to return the value of void type. The method name is main. In addition, it takes a string [] type parameter. String [] indicates that the parameter is a string array.
Example 2
void keyword
Describes how to declare and call a void method.
The following example declares a method called score and calls it to print a given score

package com.tedu.cn;

//This class is used to test how to declare and call a void method
public class Test_Method2 {
    public static void main(String[] args) {

    public static void  score(double scores){
        if (scores>=90){
        }else if (scores>=80){
        }else if (scores>=60){
        }else if (scores<60){

Compilation results

Here, the score method is a void type method, which does not return a value.
A call to a void method must be a statement. Therefore, it is called in the form of a statement on the third line of the main method, just like any statement ending with a semicolon.
Example 3
This class is used to test methods to pass parameters by value. The following example demonstrates the effect of passing by value
The program creates a method that is used to exchange two variables

package com.tedu.cn;

//This class is used to test methods that pass parameters through values
//The following example demonstrates the effect of passing by value
//The program creates a method that is used to exchange two variables
public class Test_Method3 {
    public static void main(String[] args) {
        int num1 = 2;
        int num2 = 3;
        System.out.println("Before exchange num1:"+num1+",num2:"+num2);
        //Call swap
        System.out.println("After exchange num1:"+num1+",num2:"+num2);

   public static void swap(int a,int b){
       System.out.println("\t get into swap method");
       System.out.println("\t\t Before exchange a The value of is"+a+",b The value of is:"+b);
       //Exchange the values of a and b
       int temp = a;
       a = b;
       b = temp;
       System.out.println("\t\t After exchange a The value of is:"+a+",b The value of is:"+b);

Compilation results

The swap method is called by passing two parameters. After the method is called, the value of the argument does not change.


Tags: Java MySQL Redis

Posted on Fri, 26 Nov 2021 18:16:46 -0500 by TGixer