Java Procedure
Definition: a collection of statements that perform a function together.
• a method is an ordered combination of steps to solve a class of problems
• methods contained in classes or objects
• methods are created in the program and referenced elsewhere
Format of method:
Modifier + return value type + method name + parameter type
public static void main(String []args){}
Modifier: public static
Modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method
Return value type: void
Method may return a value. retumValue Type is the data type of the return value of the method. Some methods perform the required operations but have no return value,
In this case, retumValue Type is the keyword void.
Method name: main
Is the actual name of the method. Methods and parameters together form the method name.
Parameter type: (String []args)
The parameter is like a placeholder. When a method is called, a value is passed to the parameter. This value is called an argument or variable. The parameter list refers to the parameters of the method
Number type, order, and number of parameters. Parameters are optional, and methods can contain no parameters.
Four methods of classification:
1.No parameter, no return value public void methodName(){ } 2.No parameter has a return value public int methodName(){ return 1 } 3.Return value with or without parameters public void methodName(String name){ } 4.There are parameters and return values public int methodName(String name){ return 1; }
Examples are as follows:
import java.util.Scanner; public class TestDemo { public static void main(String[] args) { max();// Call this method with no parameters and no return value int n;// Here is an acceptance max1 Integer variable of method result n. Or(int n = max1;) n = max1(); System.out.println("max1:" + n);// Output this Scanner scanner = new Scanner(System.in); System.out.print("Please enter a year:"); int y = scanner.nextInt(); boolean zhenjia = isLeapYear(y); System.out.println(zhenjia ? y + "It's a leap year" : y + "Not a leap year"); } // No parameter and no return value (this method is the easiest to understand. You can call the method directly under the main method) public static void max() { int a = 4; int b = 5; if (a > b) { System.out.println("max:" + a); } else { System.out.println("max:" + b); } } // No parameter has a return value. The return value is int integer public static int max1() { int a = 20; int b = 30; int m = 0;// Set one max Variable of m, if (a > b) { m = a;// If yes true,The maximum value is a } else { m = b;// If yes folse,The maximum value is b } return m;// What is returned here is a variable m Value of } // There are parameters and return values, and the returned value is boolean Variable of type /** * Scanner scanner = new Scanner(System.in);Reference Scanner class package * System.out.print("Please enter a year: "; standard output format * int y = scanner.nextInt();Set a variable representing the year * * Call the method with parameter y, run the boolean type value (result) returned by the result and assign it to zhenjia variable; * * boolean zhenjia = isLeapYear(y); * * System.out.println(zhenjia ? y+"Is a leap year ": y +" is not a leap year "); the output result adopts the ternary operator, * * @param year * @return */ public static boolean isLeapYear(int year) { boolean result; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { result = true;//It's a leap year. The result is true Assign to variable result; } else { result = false;//Not a leap year, the result is false Assign to variable result; } return result; }
}
Finally, add the naming rules of java
1. The package name is in lowercase, and there is only one English word with natural semantics between dot separators. The package name shall be in singular form, but if the class name has plural meaning, the class name can be in plural form.
2. The class name and interface name use the UpperCamelCase style and must follow the hump form.
3. The method name, parameter name, member variable and local variable all use lowerCamelCase style, and must follow the hump form.
4. All constant names are capitalized, and words are separated by underscores, so as to make the semantic expression complete and clear, and do not be too long.