------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
1.8 Methods
Definition of the 1.8.1 method
Definition: A block of code that completes a specific function. In many languages there are definitions of functions, whereas in Java functions are called methods.
Format:
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
Method body Statement;
return value;
}
Explain:
Modifier: Use public static now. We'll explain the other modifiers in more detail later.
Return value type: is the data type of the functional result.
Method Name: Conforms to the naming convention. Convenient for our call.
Parameter type: is the data type of the formal parameter
The formal parameter is the method definition that is used to receive the actual parameter.
Actual parameters: is actually involved in the operation.
Parameter name: The name of the variable
Method Body Statement: Is the code that completes the function.
return: Used to end the method.
Return value: Is the result of the function, which is brought to the caller by return.
To write a method, you must first clear two points: return value type and parameter list
Example 1:
Class Functiondemo{public static void Main (String [] args) {int x = 10;int y = 2;int result = SUM (x, y); SYSTEM.OUT.PRINTLN (result);} public static int sum (int a,int b) {return (A+B);}}
Operation Result:
Example 2: Enter two data in the keyboard and compare the values of two data for equality.
Import Java.util.scanner;class functiontest2{public static void Main (String [] args) {Scanner sc = new Scanner (system.in); System.out.println ("Please enter first number:"); int a = Sc.nextint (); System.out.println ("Enter second number:"); int b = Sc.nextint (); Boolean result = Compare (A, b); if (result==true) { System.out.println ("The two data you entered is equal");} else {System.out.println ("The two data you entered is not equal");}} public static Boolean compare (int a,int b) {return (a = = B);}}
Operation Result:
Example 3: Keyboard input number of rows and columns, output corresponding star
Import Java.util.scanner;class functiontest4{public static void Main (String [] args) {//Create keyboard Entry Object Scanner sc = new Scanner (S ystem.in); System.out.print ("Please enter the number of lines:"); int m = Sc.nextint (); System.out.print ("Enter Number of columns:"); int n = sc.nextint ();//call to print the Star method getxing (M,n);} public static void getxing (int m,int n) {in (int x = 1;x <= m;x++) {for (int y = 1;y <= n;y++) {System.out.print ("*");} System.out.println ();}}}
Operation Result:
Example 4: Keyboard input a data n (1<=n<=9), output corresponding nn multiplication table
Import Java.util.scanner;class functiontest5{public static void Main (String [] args) {//Create keyboard Entry Object Scanner sc = new Scanner (S ystem.in); System.out.println ("Please enter the value of N (1-9):"), int n = sc.nextint ();p RINTNN (n);} public static void Printnn (int n) {for (int x = 1;x <= n;x++) {for (int y = 1;y <= x;y++) {System.out.print (y+ "x" +x+ "=" +x *y+ ' \ t ');} System.out.println ();}}}
Operation Result:
Overloading of the 1.8.2 method
Definition: in the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of the parameter is different.
Note: Overloading is independent of the return value type.
Example:
Class functiondemo{public static void Main (string[] args) { System.out.println (Add (3,4)); System.out.println (Add (3.0,4.0)); System.out.println (Add (3,4,5)); } Addition operation, two integers and public static int add (int a, int b) { return a + b; } Addition operation, two decimal and public static double add (double A, double b) { return a + b; } Addition operation, three integers and public static int add (int a, int b,int c) { return Add (A, b) + C; }}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Dark Horse Programmer (JAVA)----basic Syntax (iv)