JAVA: Keyword Return di Java

From OnnoWiki
Jump to navigation Jump to search

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