在爪哇, 回来 是一个保留关键字,即我们不能将其用作标识符。习惯了 出口 来自一个方法,有或没有值。使用 返回关键字 由于存在以下两种方式:
- 案例1: 方法返回一个值
- 案例2: 方法不返回值
下面让我们通过直接实现它们来说明:
案例1: 方法返回一个值
对于那些 定义 返回类型、返回语句 必须是 紧接着是返回值。
例子:
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 )); } } |
6.0
输出说明: 什么时候 我们正在调用一个类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 } |
1No return keyword is used and program executed successfully
注: 返回语句不是必需的( 但是可以使用 )对于返回类型为void的方法。我们可以使用“return”也就是说 不归还任何东西 .
#2: 方法 无效返回类型
例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
输出说明: 如果声明 如果(j<9) 那么控制是真的吗 出口 从方法和行为上 不 执行RR方法语句的其余部分,然后再次返回 main()方法 .
现在继续前进,极客,你一定想知道如果我们在程序末尾使用return语句会怎么样?
return语句可以在方法中的不同位置使用,但我们需要确保它必须是在方法中执行的最后一条语句。
笔记 :返回语句 不必是方法中的最后一条语句,但必须是要执行的最后一条语句 用一种方法。
例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) 变成现实,它就会执行 回来 语句,因此流就来了 出来 使用“demofunction”方法,然后再次返回main。接下来,是 返回语句 必须是方法中要执行的最后一条语句,这意味着 没有 返回后定义任何代码的要点如下:
例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.0Program Executed Successfully
注: 在上面的程序中,我们使用uncomment语句,它会抛出一个错误。
本文由 拉贾特·拉瓦特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。