Java中的return关键字
在Java中, return是一个保留关键字,即我们不能将它用作标识符。它用于退出一个方法,有或没有一个值。 return 关键字的使用有以下两种方式:
- 案例 1:返回值的方法
- 案例 2:方法不返回值
让我们通过直接实现它们来说明如下:
案例 1:返回值的方法
For methods that define a return type, return statement must be immediately followed by return value.
例子:
Java
// Java Program to Illustrate Usage of return Keyword
// Main method
class GFG {
// Method 1
// Since return type of RR method is double
// so this method should return double value
double RR(double a, double b) {
double sum = 0;
sum = (a + b) / 2.0;
// Return statement as we already above have declared
// return type to be double
return sum;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(new GFG().RR(5.5, 6.5));
}
}
Java
// Java program to illustrate no return
// keyword needed inside void method
// Main class
class GFG {
// Since return type of RR method is
// void so this method shouldn't return any value
void demoSum(int a, int b)
{
int sum = 0;
sum = (a + b) / 10;
System.out.println(sum);
// No return statement in this method
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling the method
// Over custom inputs
new GFG().demoSum(5, 5);
// Display message on the console for successful
// execution of the program
System.out.print(
"No return keyword is used and program executed successfully");
}
// Note here we are not returning anything
// as the return type is void
}
Java
// Java program to illustrate usage of
// return keyword in void method
// Class 1
// Main class
class GFG {
// Method 1
// Since return type of RR method is
// void so this method should not return any value
void demofunction(double j)
{
if (j < 9)
// return statement below(only using
// return statement and not returning
// anything):
// control exits the method if this
// condition(i.e, j<9) is true.
return;
++j;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling above method declared in above class
new GFG().demofunction(5.5);
// Display message on console to illustrate
// successful execution of program
System.out.println("Program executed successfully");
}
}
Java
// Java program to illustrate return must not be always
// last statement, but must be last statement
// in a method to execute
// Main class
class GFG {
// Method 1
// Helper method
// Since return type of RR method is void
// so this method should not return any value
void demofunction(double i)
{
// Demo condition check
if (i < 9)
// See here return need not be last
// statement but must be last statement
// in a method to execute
return;
else
++i;
}
// Method 2
// main driver method
public static void main(String[] args)
{
// Calling the method
new GFG().demofunction(7);
// Display message to illustrate
// successful execution of program
System.out.println("Program executed successfully");
}
}
Java
// Java program to illustrate usage of
// statement after return statement
// Main class
class GFG {
// Since return type of RR method is void
// so this method should return any value
// Method 1
void demofunction(double j)
{
return;
// Here get compile error since can't
// write any statement after return keyword
++j;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling the above defined function
new GFG().demofunction(5);
}
}
Java
// Java program to illustrate usage
// of return keyword
// Main class
class GFG {
// Since return type of RR method is
// void so this method should not return any value
// Method 1
void demofunction(double val)
{
// Condition check
if (val < 0) {
System.out.println(val);
return;
// System.out.println("oshea");
}
else
++val;
}
// Method 2
// Main drive method
public static void main(String[] args)
{
// CAlling the above method
new GFG().demofunction(-1);
// Display message to illustrate
// successful execution of program
System.out.println("Program Executed Successfully");
}
}
6.0
时间复杂度: O(1)
辅助空间: O(1)
输出说明:当 我们正在调用一个具有返回 sum的类 GFG 方法,该方法返回 sum 的值,并且该值将显示在控制台上。
案例 2:方法不返回值
对于不返回值的方法,可以跳过Java中的 return 语句。这里有两种情况,当用户没有返回值时,如下所示:
- #1:在 void函数中不使用 return 语句的方法
- #2:返回类型为 void 的方法
#1:在 void函数中不使用 return 语句的方法
例子
Java
// Java program to illustrate no return
// keyword needed inside void method
// Main class
class GFG {
// Since return type of RR method is
// void so this method shouldn't return any value
void demoSum(int a, int b)
{
int sum = 0;
sum = (a + b) / 10;
System.out.println(sum);
// No return statement in this method
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling the method
// Over custom inputs
new GFG().demoSum(5, 5);
// Display message on the console for successful
// execution of the program
System.out.print(
"No return keyword is used and program executed successfully");
}
// Note here we are not returning anything
// as the return type is void
}
1
No return keyword is used and program executed successfully
Note: Return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.
#2:返回类型为 void的方法
示例 1-A:
Java
// Java program to illustrate usage of
// return keyword in void method
// Class 1
// Main class
class GFG {
// Method 1
// Since return type of RR method is
// void so this method should not return any value
void demofunction(double j)
{
if (j < 9)
// return statement below(only using
// return statement and not returning
// anything):
// control exits the method if this
// condition(i.e, j<9) is true.
return;
++j;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling above method declared in above class
new GFG().demofunction(5.5);
// Display message on console to illustrate
// successful execution of program
System.out.println("Program executed successfully");
}
}
Program executed successfully
输出说明:如果语句if(j<9)为真,则控制退出该方法并且不执行 RR 方法的其余语句,因此再次返回到main() 方法。
现在继续前进,你一定想知道如果我们在程序末尾使用 return 语句会怎样?
return 语句可以在方法的各个地方使用,但我们需要确保它必须是在方法中执行的最后一条语句。
Note: return statement need not to be last statement in a method, but it must be last statement to execute in a method.
示例 1-B:
Java
// Java program to illustrate return must not be always
// last statement, but must be last statement
// in a method to execute
// Main class
class GFG {
// Method 1
// Helper method
// Since return type of RR method is void
// so this method should not return any value
void demofunction(double i)
{
// Demo condition check
if (i < 9)
// See here return need not be last
// statement but must be last statement
// in a method to execute
return;
else
++i;
}
// Method 2
// main driver method
public static void main(String[] args)
{
// Calling the method
new GFG().demofunction(7);
// Display message to illustrate
// successful execution of program
System.out.println("Program executed successfully");
}
}
Program executed successfully
输出说明:
当条件(i<9)为真时,它执行return语句,因此流程从“demofunction”方法中出来并再次返回到 main。在此之后, return 语句必须是方法中执行的最后一条语句,这意味着在 return 之后定义任何代码是没有意义的,如下所述:
示例 2A
Java
// Java program to illustrate usage of
// statement after return statement
// Main class
class GFG {
// Since return type of RR method is void
// so this method should return any value
// Method 1
void demofunction(double j)
{
return;
// Here get compile error since can't
// write any statement after return keyword
++j;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Calling the above defined function
new GFG().demofunction(5);
}
}
输出:
示例 2-B
Java
// Java program to illustrate usage
// of return keyword
// Main class
class GFG {
// Since return type of RR method is
// void so this method should not return any value
// Method 1
void demofunction(double val)
{
// Condition check
if (val < 0) {
System.out.println(val);
return;
// System.out.println("oshea");
}
else
++val;
}
// Method 2
// Main drive method
public static void main(String[] args)
{
// CAlling the above method
new GFG().demofunction(-1);
// Display message to illustrate
// successful execution of program
System.out.println("Program Executed Successfully");
}
}
-1.0
Program Executed Successfully
Note: In the above program we do uncomment statements it will throw an error.