---restore content starts---
Definition of the method:
In general, defining a method contains the following syntax:
Modifier returns a value type method name (parameter type parameter name, ...) { ... Method Body ... return value;}
modifier : Public,private,default,protect (represents the permission that the method is called by another method object)
return value type:int,long,short,double,float,string (indicates the data type of the method return value) void (void if no return value is applied)
method Name: is the actual name of the method. The method name and the parameter list together form the method signature.
parameter type: The data type of the parameter
method Body: The method body contains specific statements that define the specific functions of the method.
Method is called:
Java supports two ways of calling methods, depending on whether the method returns a value.
1. When a method returns a value, the method call is usually treated as a value or a value expression. For example:
int max = Getmax (30,40);
2. if the return value of the method is void, the method invocation must be a statement form. For example: println () returns void. The following calls are made in the form of a statement:
System.out.println ("Welcome to java!");
Let's give you two examples:
1. There is a return value method:
Package min;
Public class demo{
Public static void Main (string[] args) {
int i = 5,J = 2;
int k = min (i,j);
System.out.println ("This minimum between" +i+ "and" +j+ "is" +k);//Printing results
}
public static int min (int num1,int num2) {//define a method to obtain the minimum value
int result;
if (num1<num2) result = num1;//Compares two values to obtain a minimum value and assigns a value to result
else result = num2;
return result;//Returns the result value
}
}
The results of the above instance compilation run as follows:
This minimum between 5 and 2 are 2
This program contains the main method and the Min method. The main method is called by the JVM, except that there is no difference between the main method and other methods.
The header of the main method is invariant, with modifier public and static, return void type value, method name is main, plus a string[] indicates that the parameter is Zifu
2. No return value method:
Package Score;public class demo{public static void Main (str1ing[] args) { printclass (78);//method calls in standalone statement }< C5/>public static void Printclass (double score) { if (score >=90.0) System.out.println (' A ');// Score score is greater than 90.0 output ' A ' else if (score >= 80.0) System.out.println (' B ');//score score is greater than 80.0 output ' B ' else if ( Score >= 70.0) System.out.println (' C ');//score score greater than 70.0 output ' C ' else if (score >= 60.0) System.out.println (' d ');//score score greater than 60.0 output ' d ' else System.out.println (' f ');//Other case output ' F ' }}
The result of compiling the above example is: C
The Printclass method here is a void type method, which does not return a value. A call to a void method must be a standalone statement, and a method that has a return value can appear in an expression to complete the call. So, here the Printclass method is called in the form of a statement in line 2nd of the Main method.
(10.23) Java trivia!