1 Public classTestreturn2 { 3 4 Public Static voidMain (String args[])5 { 6Testreturn T =NewTestreturn ();7 T.test1 ();8 System.out.println (T.test2 ());9 intSum=getsum (4,6);TenSystem.out.println ("sum=" +sum); OneGet (4,5); A } - - the Public voidtest1 () - { -System.out.println ("---------return statement with no return value type Test--------"); - for(inti = 1;; i++) + { - if(i = = 4)return; +System.out.println ("i =" +i); A } at } - - PublicString test2 () - { - - return"Returns a string"; in } - to + - Public Static intGetsum (intAintb) the { * $ returnA +b;Panax Notoginseng - } the + Public Static voidGetintAintb) A { theSystem.out.println (A +b); + return ; - } $ $ -}
Run results
Declaration of method: Modifier returns type method name (parameter ...)
The return type specifies the type of the amount added after return, and if the return type is declared void, the return value is not required
public static void Main ()//Program entry can only be declared as void and int return
{
No need to return a value just OK
}
public static void mm ()
{
return;//sometimes, in void method, you need to jump out of it, you can use return directly and not add any amount in the back
}
public static int cc ()
{
Return 321;//returns an integer because the return type is declared as int
}
public static string Msmsm ()
{
return "ASDFHASJGHDG"; Returns a string because the return type is declared as String
}
Return this method is often used in functions, and the usage is more classical.
Here's where I'll take note of the use of return:
Usage 1: Returns the value of the function:
Example 1: I'm going to compile a function of the computational hierarchy:
function JC (n) {
var j=1;
for (Var i=0;i<=n;i++) {
J*=i;
}
Return J;
}
Alert (JC (5));
This calculates 5!, the output is 120;
The function returned here is the value of the return function;
Example 2: I now make up another function of the computational hierarchy:
function JC (n) {
var j=1;
for (var i=0;;) {
J*=++i;
if (i==n) {return J;}
}
}
Alert (JC (5));
This calculates 5!, the output is 120;
The return function here is to jump out of the loop and jump out of the function, returning the function value;
Example 3: Another return example
Function cs (n) {
if (n==1) {Retrun 1;}
if (n==2) {return;}
else{}
}
Description: When the argument is 1 o'clock the function returns a value of 1 and jumps out of the function,
When the argument is 2 o'clock the return value of the function is empty and the function jumps out,
When the function is a different value, continue executing the statement below the function until the next return is encountered or all of the statements are out of function
The above example shows that return returns the value of a function and jumps out of the function;
The above example only shows the general purpose of return, and below I have a place to pay attention to;
Returns can return many data types, so far I have not found what he can not return, hehe
Included in the operation of the DOM, for example, or give examples;
Example 4:
<body>
<div id= "KK" > AA </div>
</body>
<script>
Function cs (IDs) {
return document.getElementById (IDS). Firstchild.nodevalue;
}
Alert (CS ("KK"));
The value in the div is returned as AA;
Alert (document.getElementById (KK). Firstchildnodevalue);
Although the value returned in the DIV at this time is also AA;
But they're not the same, you know why???
Oh, in the example:
document.getElementById (IDS). firstchild.nodevalue= "BB";
Then the value in the div will be changed from AA to BB;
As we all know,
When CS ("KK") = "BB";
Do you think you can also change the value of div from AA to BB?
Oh, here, you will understand that the return of the object can not be assigned to the operation;
This means CS ("KK") = "BB";
Here is what I want to say return to pay attention to the problem!!!
More Questions to study ...
</script>
Use of return in Java