Difference between revisions of "JAVA: Keyword Return di Java"

From OnnoWiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
In Java, return is a reserved keyword i.e, we can’t use it as an identifier. It is used to exit from a method, with or without a value. Usage of return keyword as there exist two ways as listed below as follows:
+
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:
 
+
Di Java, return adalah kata kunci yang dicadangkan yaitu, kami tidak dapat menggunakannya sebagai pengidentifikasi. Digunakan untuk keluar dari metode, dengan atau tanpa nilai. Penggunaan kata kunci kembali karena ada dua cara seperti yang tercantum di bawah ini sebagai berikut:
+
  Case 1: Method returning sebuah nilai
 
+
  Case 2: Method tidak returning sebuah nilai
 
+
  Case 1: Methods returning a value
 
  Case 2: Methods not returning a value
 
 
 
Let us illustrate by directly implementing them as follows:
 
 
 
 
Mari kita ilustrasikan dengan mengimplementasikannya secara langsung sebagai berikut:
 
Mari kita ilustrasikan dengan mengimplementasikannya secara langsung sebagai berikut:
  
  
==Case 1: Methods returning a value==
+
==Case 1: Method returning sebuah value==
 
 
For methods that define a return type, return statement must be immediately followed by return value.
 
 
 
Untuk metode yang mendefinisikan tipe pengembalian, pernyataan pengembalian harus segera diikuti dengan nilai pengembalian.
 
 
 
  
 +
Untuk method yang mendefinisikan tipe return, pernyataan return harus segera diikuti dengan nilai return.
  
 
Contoh:
 
Contoh:
Line 51: Line 42:
 
  6.0
 
  6.0
  
Output explanation: When we are calling a class GFG method that has return sum which returns the value of sum and that’s value gets displayed on the console.
+
Penjelasan Output: Saat kita memanggil metode kelas GFG yang memiliki return sum yang return nilai jumlah dan nilai itu akan ditampilkan di konsol.
 
 
Penjelasan keluaran: Saat kita memanggil metode kelas GFG yang memiliki jumlah kembalian yang mengembalikan nilai jumlah dan nilai itu akan ditampilkan di konsol.
 
  
  
==Case 2: Methods not returning a value==
+
==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:
+
Untuk method yang tidak return nilai, pernyataan retyrn di Java dapat dilewati. di sini muncul dua (2) kasus ketika tidak ada nilai yang di return oleh pengguna seperti yang tercantum di bawah ini sebagai berikut:
  
  #1: Method not using return statement in void function
+
  #1: Method tidak menggunakan return statement di void function
  #2: Methods with return type void  
+
  #2: Method dengan return type void
  #1: Method not using return statement in void function
+
   
 +
===#1: Method tidak menggunakan return statement di void function===
  
 
Contoh:
 
Contoh:
Line 104: Line 94:
 
  1
 
  1
  
No return keyword is used and program executed successfully
+
Tidak ada return keyword yang digunakan dan program berhasil dijalankan
Note: Return statement not required (but can be used) for methods with return type void. We can use “return;” which means not return anything.  
+
Catatan: Pernyataan return tidak diperlukan (tetapi dapat digunakan) untuk method dengan tipe return void. Kita bisa menggunakan "return;" yang berarti tidak return apa pun.
  
#2: Methods with void return type
+
===#2: Method dengan return type void===
  
 
Contoh 1-A:
 
Contoh 1-A:
Line 150: Line 140:
 
  Program executed successfully
 
  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.
+
Penjelasan Output: Jika pernyataan if(j<9) true maka kontrol keluar dari method dan tidak mengeksekusi sisa pernyataan method RR dan karenanya kembali lagi ke method main().
 +
 
 +
Mari bergerak maju, kita pasti bertanya-tanya bagaimana jika kita menggunakan pernyataan return di akhir program?
  
Now moving ahead geek you must be wondering what if we do use return statement at the end of the program?
+
pernyataan return dapat digunakan di berbagai tempat dalam method tetapi kita perlu memastikan bahwa itu harus menjadi pernyataan terakhir untuk dieksekusi dalam suatu method.
  
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.
+
Catatan: pernyataan return tidak harus menjadi pernyataan terakhir dalam suatu method, tetapi harus pernyataan terakhir untuk dieksekusi dalam suatu 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:
 
Contoh 1-B:
Line 201: Line 192:
 
  Program executed successfully
 
  Program executed successfully
  
Output explanation:  
+
Penjelasan Output:  
  
 
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:
 
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:
 +
 +
Saat kondisi (i<9) menjadi true, ia menjalankan perintah return, dan karenanya aliran program keluar dari method 'demofunction' dan kembali lagi ke main. Setelah ini, perintah return harus menjadi perintah terakhir yang dieksekusi dalam suatu method, yang berarti tidak ada gunanya mendefinisikan code apa pun setelah return yang dijelaskan di bawah ini sebagai berikut:
 +
  
 
Contoh 2A
 
Contoh 2A
Line 238: Line 232:
 
Output:
 
Output:
  
 
+
++j
 +
  ^
 +
1 error
  
 
Contoh 2-B
 
Contoh 2-B
Line 282: Line 278:
 
Output
 
Output
 
  -1.0
 
  -1.0
 +
Program Executed Successfully
  
Program Executed Successfully
+
Catatan: Pada program di atas kita melakukan uncomment perintah maka akan muncul error.
Note: In the above program we do uncomment statements it will throw an error.
 
 
 
  
 
==Referensi==
 
==Referensi==
  
 
* https://www.geeksforgeeks.org/return-keyword-java/
 
* https://www.geeksforgeeks.org/return-keyword-java/

Latest revision as of 05:27, 6 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

Untuk method yang tidak return nilai, pernyataan retyrn di Java dapat dilewati. di sini muncul dua (2) kasus ketika tidak ada nilai yang di return oleh pengguna seperti yang tercantum di bawah ini sebagai berikut:

#1: Method tidak menggunakan return statement di void function
#2: Method dengan return type void

#1: Method tidak menggunakan return statement di 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

Tidak ada return keyword yang digunakan dan program berhasil dijalankan Catatan: Pernyataan return tidak diperlukan (tetapi dapat digunakan) untuk method dengan tipe return void. Kita bisa menggunakan "return;" yang berarti tidak return apa pun.

#2: Method dengan return type void

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

Penjelasan Output: Jika pernyataan if(j<9) true maka kontrol keluar dari method dan tidak mengeksekusi sisa pernyataan method RR dan karenanya kembali lagi ke method main().

Mari bergerak maju, kita pasti bertanya-tanya bagaimana jika kita menggunakan pernyataan return di akhir program?

pernyataan return dapat digunakan di berbagai tempat dalam method tetapi kita perlu memastikan bahwa itu harus menjadi pernyataan terakhir untuk dieksekusi dalam suatu method.

Catatan: pernyataan return tidak harus menjadi pernyataan terakhir dalam suatu method, tetapi harus pernyataan terakhir untuk dieksekusi dalam suatu 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

Penjelasan Output:

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:

Saat kondisi (i<9) menjadi true, ia menjalankan perintah return, dan karenanya aliran program keluar dari method 'demofunction' dan kembali lagi ke main. Setelah ini, perintah return harus menjadi perintah terakhir yang dieksekusi dalam suatu method, yang berarti tidak ada gunanya mendefinisikan code apa pun setelah return yang dijelaskan di bawah ini sebagai berikut:


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:

++j
  ^
1 error

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

Catatan: Pada program di atas kita melakukan uncomment perintah maka akan muncul error.

Referensi