Two functions of return in Java:
(1) Returns the value of the specified type of the method (this value is always determined), or it can be an object
(2) The end of the method
Format: return[() [expression][]];
Two forms: (1) There are return types for example: returns I;
(2) no return type returns;
Example
1.public static void Main (string[] args) {
System.out.println ("---------no return statement test--------" of the returned value type);
for (int i = 1;i <= i++) {
if (i = = 4) return;
System.out.println ("i =" + i);
}
}
Output:---------Return statement test with no returned value type--------
I=1
i=2
I=3
2.public class Returntest {
public static void Main (string[] args) {
System.out.println ("---------no return statement test--------" of the returned value type);
for (int i = 1;i <= i++) {
if (i = = 4) return;
System.out.println ("i =" + i);
}
New Returntest (). T ();
}
public int T () {
int i;
System.out.println ("---------have return statement test--------" of the returned value type);
for (i = 1;i <= i++) {
if (i = = 4) return i;
System.out.println ("i =" + i);
}
return i;
}
}
Output:---------Return statement test with no returned value type--------
i = 1
i = 2
i = 3
Reason: Return function to End method
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service