Difference between revisions of "JAVA: Keyword Return di Java"
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | Di Java, return adalah kata kunci yang di reserved artinya, kita tidak dapat menggunakannya sebagai pengidentifikasi. Digunakan untuk return dari method, dengan atau tanpa nilai. Penggunaan keyword return karena ada dua (2) cara seperti yang tercantum di bawah ini sebagai berikut: | |
− | |||
− | + | Case 1: Method returning sebuah nilai | |
− | Case 1: | + | Case 2: Method tidak returning sebuah nilai |
− | Case 2: | ||
− | |||
− | |||
Mari kita ilustrasikan dengan mengimplementasikannya secara langsung sebagai berikut: | Mari kita ilustrasikan dengan mengimplementasikannya secara langsung sebagai berikut: | ||
− | ==Case 1: | + | ==Case 1: Method returning sebuah value== |
− | |||
− | |||
− | |||
− | |||
− | |||
+ | Untuk method yang mendefinisikan tipe return, pernyataan return harus segera diikuti dengan nilai return. | ||
Contoh: | Contoh: | ||
Line 51: | Line 43: | ||
6.0 | 6.0 | ||
− | Output | + | Penjelasan Output: Saat kita memanggil metode kelas GFG yang memiliki return sum yang return nilai jumlah dan nilai itu akan ditampilkan di konsol. |
− | |||
− | |||
− | ==Case 2: | + | ==Case 2: Method tidak returning sebuah value== |
For methods that do not return a value, return statement in Java can be skipped. here there arise two cases when there is no value been returned by the user as listed below as follows: | For methods that do not return a value, return statement in Java can be skipped. here there arise two cases when there is no value been returned by the user as listed below as follows: |
Revision as of 06:54, 5 May 2022
Di Java, return adalah kata kunci yang di reserved artinya, kita tidak dapat menggunakannya sebagai pengidentifikasi. Digunakan untuk return dari method, dengan atau tanpa nilai. Penggunaan keyword return karena ada dua (2) cara seperti yang tercantum di bawah ini sebagai berikut:
Case 1: Method returning sebuah nilai Case 2: Method tidak returning sebuah nilai
Mari kita ilustrasikan dengan mengimplementasikannya secara langsung sebagai berikut:
Case 1: Method returning sebuah value
Untuk method yang mendefinisikan tipe return, pernyataan return harus segera diikuti dengan nilai return.
Contoh:
// 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)); } }
Output
6.0
Penjelasan Output: Saat kita memanggil metode kelas GFG yang memiliki return sum yang return nilai jumlah dan nilai itu akan ditampilkan di konsol.
Case 2: Method tidak returning sebuah value
For methods that do not return a value, return statement in Java can be skipped. here there arise two cases when there is no value been returned by the user as listed below as follows:
#1: Method not using return statement in void function #2: Methods with return type void #1: Method not using return statement in void function
Contoh:
// 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 }
Output
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: Methods with void return type
Contoh 1-A:
// 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"); } }
Output
Program executed successfully
Output explanation: If the statement if(j<9) is true then control exits from the method and does not execute the rest of the statement of the RR method and hence comes back again to main() method.
Now moving ahead geek you must be wondering what if we do use return statement at the end of the program?
return statement can be used at various places in the method but we need to ensure that it must be the last statement to get executed in a method.
Note: return statement need not to be last statement in a method, but it must be last statement to execute in a method.
Contoh 1-B:
// 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"); } }
Output
Program executed successfully
Output explanation:
As the condition (i<9) becomes true, it executes return statement, and hence flow comes out of ‘demofunction’ method and comes back again to main. Following this, the return statement must be the last statement to execute in a method, which means there is no point in defining any code after return which is clarified below as follows:
Contoh 2A
// 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); } }
Output:
Contoh 2-B
// 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"); } }
Output
-1.0
Program Executed Successfully Note: In the above program we do uncomment statements it will throw an error.