Return value
Understanding in reality
Go to the bank to withdraw money
Enter account number when withdrawing money ----- > call method
Enter the number of withdrawals ----- > give actual parameters
The money given to you by the bank is completed, and the money given to you by the bank is the return value
The keyword to be used in the return value in the code
Return: return
Function: ends the execution of the current method
After returning the data after return, in addition to the method, the data of the return value and the method declaration position are required to inform the caller that the data type of the return value is consistent [data type consistency]
Complete a method with no parameter and return value
demand give me five Return a 5 Method analysis: Fixed format: public static Don't ask Return value type [key]: The return value is a 5. What data type is 5? int type Method name: giveMeFive Formal parameter list: Here, a data 5 is returned, and no parameters are required () Method statement: public static int giveMeFive()
Implementation of method
/* @return In the document comments, the caller is informed of the type, interpretation and meaning of the return value of the current method */ /** * This method returns a 5 and an integer * * @return Returns an integer of 5 */ public static int giveMeFive() { return 5; }
Method call
class Method1 { public static void main(String[] args) { /* Call a method with a return value How to use the return value??? Assignment, operation, printing The return value of the method is returned to the outside, and the caller determines the value of the current result Usage. */ int ret = 0; System.out.println("Before calling the method ret : " + ret); ret = giveMeFive(); System.out.println("Direct call method:" + giveMeFive()); System.out.println("After calling the method ret : " + ret); } /** * This method returns a 5 and an integer * * @return Returns an integer of 5 */ public static int giveMeFive() { return 5; } }
Small summary
- Keyword used for return value return keyword
- For a method with a return value, you need to add @ return in the document comment to inform the caller of the type and meaning of the return value of the method
- The type of the returned value of the method must be consistent with the type of the returned data in the method body. If it is inconsistent, an error will be reported
Implement a method with parameters and return values
//Implement a method with parameters and return values public static long num2(int one,int two) { return one+two; }
/* *Here, a long data type is defined, and the parameter is int data type. In order to prevent the return value * from exceeding the int data type */ long number1=num2(900,910);//The data type of the return value is long. Pay attention to the conversion of data types System.out.println(number1);
summary
Calling a method with multiple parameters requires that the data type, number and order of the parameters passed in must be consistent with the method declaration
Implement a method with no parameters and no return value
package Arrays; public class Mite7 { public static void main(String[] args) { // TODO Auto-generated method stub //Implement a method with no parameters and no return value arrays(); } public static void arrays() { for(int a=0;a<10;a++) { System.out.println(a); } } }
summary
For methods with no return value, you can only use separate calls, not print calls or assignment calls
Implement a method call with parameters and no return value
package Arrays; public class Mite7 { public static void main(String[] args) { // TODO Auto-generated method stub //Implement a method with parameters and no return value arrays(12,34);// } public static void arrays(int c,int d) { System.out.println(c+d); } }
Non static call with return value in formal parameter
demo01 b=new demo01();//Class instantiation System.out.println(b.add(3, 6)); } //Formal parameters are equivalent to placeholders public int add(int a,int b) { return a+b; }
Static call with return value in formal parameter
int add=demo01.add(6,9); System.out.println(add); } public static int add(int a,int b) { return a+b; }
Formal parameters shall correspond to actual parameters one by one