Java Basic Syntax---java method 0. Overview
Method: is to solve a kind of problem code of the orderly combination, is a function module;
In the method section, only the object noun is used to invoke the method;
Scanner sc = new Scanner(System.in);//sc对象名sc.nextInt();sc.next();public static int age(int birthday){...} //单个参数static double interest(double prinncipal, int year){...} //多个参数// 在一些其他语言中,方法指`过程和函数`。// 返回void类型返回值的方法称为过程;// 返回非void类型返回值的方法称为函数;System.out.println();// System 是一个系统类// out 是一个标准输出对象// println() 是一个方法;// 意思为:调用系统类 System 中的标准输出对象 out 中的方法 println()。
When object-oriented, static methods are called using the class name;
What is a method
A Java method is a collection of statements that perform a function together;
- The method is an orderly combination of code to solve a kind of problem;
- The method is contained in a class or object;
- Method is created in the program and is called elsewhere;
Advantages of the method
- Make the program brief and clear;
- Facilitate the maintenance of the program;
- Can improve the development efficiency of the program;
- Improve the reuse rate of code;
Naming rules for methods
- The first word of the name of the method should begin with a lowercase letter, and the following words are written in uppercase letters without using a connector. For example: Addperson.
- The underscore may appear in the JUnit test method name to separate the logical components of the name. A typical pattern is: Test _ , for example Testpop_emptystack.
Declaration/definition of a method
访问修饰符 返回值类型 方法名 (参数列表(参数类型 参数名)){ 方法体}例如:public static void main(String[] args){ System.out.println("Hello,Ryanjie");}
- access modifier: modifier, which is optional, tells the compiler how to call the method. Defines the type of access for the method.
- return value type: The method may return a value. Returnvaluetype is the data type of the method return value. Some methods perform the required operations, but do not return a value. In this case, Returnvaluetype is the keyword void.
- method Name: is the actual name of the method. The method name and the parameter table together form the method signature.
- parameter type: The parameter is like a placeholder. When the method is called, the value is passed 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 can contain no parameters.
- method Body: The method body contains a specific statement that defines the function of the method.
Eg: defines a maxnum method that returns a larger value in two parameters.
/* 返回两个整型变量数据中的较大值 */public static int maxnum(int num1, int num2){ int maxn = num1; if(num1 < num2) { maxn = num2; } return maxn;}
Invocation of the method
Java
Method classification
The method can be divided into four classes depending on whether the method has parameters, whether it is the same as the return value:
- No parameter no return value method
- Non-parametric return value method
- No return value method with parameter
- With parameter return value method
1. No parameter no return value method
To customize a method that has no parameters and no return values:
输出:******** ********欢迎来到Java的世界!******** ********public class MethodTest { public void printStar(){ //打印星号 System.out.println("******** ********"); } public static void main(String[] args) { //创建一个MethodTest类的对象myMethodTest MethodTest myMethodTest = new MethodTest(); //使用对象名.方法名()去调用方法 myMethodTest.printStar(); System.out.println("欢迎来到Java的世界!"); myMethodTest.printStar(); }}
2. Non-parametric return value method
Scanner class Next () method, the return value is a string type
To customize a method with no parameter return values:
public class RectangleArea { //自定义一个返回长方体面积的方法 public double area(){ double length = 6.66; double width = 8.88; double getArea = length * width; return getArea; } public static void main(String[] args) { RectangleArea rc = new RectangleArea(); System.out.println("长方体的面积为:" + rc.area()); }}Output:长方体的面积为:59.140800000000006
3. No return value method with parameters
Defines a method that asks for the maximum value of two double type data, and prints the maximum value in the method.
public void maxnum(double num1 , double num2){}
public class MaxnumberTest { //定义一个打印两个数中的较大值的方法 public void maxnum(double num1 , double num2){ double maxn; maxn = num1; if(num2 > maxn) { maxn = num2; } System.out.println("两个数 " + num1 + " 和 " + num2 + " 中的较大值为:" + maxn); } public static void main(String[] args) { MaxnumberTest maxnumber = new MaxnumberTest(); double number1 = 2.135; double number2 = 6.3356; maxnumber.maxnum(number1 , number2); }}Output: 两个数 2.135 和 6.3356 中的较大值为:6.3356
Java Basic Syntax---java method