Difference between revisions of "JAVA: Method di Java"
Onnowpurbo (talk | contribs) |
Onnowpurbo (talk | contribs) |
||
Line 40: | Line 40: | ||
Jumlah parameter adalah 2, type parameter adalah int. | Jumlah parameter adalah 2, type parameter adalah int. | ||
− | == | + | ==Bagaimana Menamakan Method?== |
A method name is typically a single word that should be a verb in lowercase or multi-word, that begins with a verb in lowercase followed by an adjective, noun….. After the first word, the first letter of each word should be capitalized. | A method name is typically a single word that should be a verb in lowercase or multi-word, that begins with a verb in lowercase followed by an adjective, noun….. After the first word, the first letter of each word should be capitalized. | ||
− | + | Nama metode biasanya satu kata yang harus menjadi kata kerja dalam huruf kecil atau multi-kata, yang dimulai dengan kata kerja dalam huruf kecil diikuti oleh kata sifat, kata benda….. Setelah kata pertama, huruf pertama dari setiap kata harus dikapitalisasi . | |
− | While defining a method, remember that the method name must be a verb and start with a lowercase letter. | + | |
− | If the method name has more than two words, the first name must be a verb followed by an adjective or noun. | + | ===Rules to Name a Method=== |
− | In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example, findSum, computeMax, setX and getX. | + | |
+ | * While defining a method, remember that the method name must be a verb and start with a lowercase letter. | ||
+ | * If the method name has more than two words, the first name must be a verb followed by an adjective or noun. | ||
+ | * In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example, findSum, computeMax, setX and getX. | ||
Generally, a method has a unique name within the class in which it is defined but sometimes a method might have the same name as other method names within the same class as method overloading is allowed in Java. | Generally, a method has a unique name within the class in which it is defined but sometimes a method might have the same name as other method names within the same class as method overloading is allowed in Java. | ||
− | Method Calling | + | ===Method Calling=== |
+ | |||
The method needs to be called for using its functionality. There can be three situations when a method is called: | The method needs to be called for using its functionality. There can be three situations when a method is called: | ||
A method returns to the code that invoked it when: | A method returns to the code that invoked it when: | ||
− | It completes all the statements in the method | + | * It completes all the statements in the method |
− | It reaches a return statement | + | * It reaches a return statement |
− | Throws an exception | + | * Throws an exception |
− | + | ||
+ | |||
+ | ==Contoh 1:== | ||
− | // Java Program to Illustrate Methods | + | // Java Program to Illustrate Methods |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | // Importing required classes | |
− | + | import java.io.*; | |
− | |||
− | |||
− | |||
+ | // Class 1 | ||
+ | // Helper class | ||
+ | class Addition { | ||
+ | // Initially taking sum as 0 | ||
+ | // as we have not started computation | ||
+ | int sum = 0; | ||
+ | |||
+ | // Method | ||
+ | // To add two numbers | ||
+ | public int addTwoInt(int a, int b) | ||
+ | { | ||
+ | |||
+ | // Adding two integer value | ||
+ | sum = a + b; | ||
+ | |||
+ | // Returning summation of two values | ||
+ | return sum; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Class 2 | ||
+ | // Helper class | ||
+ | class GFG { | ||
+ | |||
+ | // Main driver method | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | |||
+ | // Creating object of class 1 inside main() method | ||
+ | Addition add = new Addition(); | ||
+ | |||
+ | // Calling method of above class | ||
+ | // to add two integer | ||
+ | // using instance created | ||
+ | int s = add.addTwoInt(1, 2); | ||
+ | |||
+ | // Printing the sum of two numbers | ||
+ | System.out.println("Sum of two integer values :" | ||
+ | + s); | ||
+ | } | ||
+ | } | ||
+ | |||
Output | Output | ||
− | Sum of two integer values :3 | + | * Sum of two integer values :3 |
− | + | ==Contoh 2:== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | // Java Program to Illustrate Method Calling | ||
+ | // Via Different Ways of Calling a Method | ||
+ | |||
+ | // Importing required classes | ||
+ | import java.io.*; | ||
+ | // Class 1 | ||
+ | // Helper class | ||
+ | class Test { | ||
+ | |||
+ | public static int i = 0; | ||
+ | |||
+ | // Constructor of class | ||
+ | Test() | ||
+ | { | ||
+ | |||
+ | // Counts the number of the objects of the class | ||
+ | i++; | ||
+ | } | ||
+ | |||
+ | // Method 1 | ||
+ | // To access static members of the class and | ||
+ | // and for getting total no of objects | ||
+ | // of the same class created so far | ||
+ | public static int get() | ||
+ | { | ||
+ | |||
+ | // statements to be executed.... | ||
+ | return i; | ||
+ | } | ||
+ | |||
+ | // Method 2 | ||
+ | // Instance method calling object directly | ||
+ | // that is created inside another class 'GFG'. | ||
+ | |||
+ | // Can also be called by object directly created in the | ||
+ | // same class and from another method defined in the | ||
+ | // same class and return integer value as return type is | ||
+ | // int. | ||
+ | public int m1() | ||
+ | { | ||
+ | |||
+ | // Display message only | ||
+ | System.out.println( | ||
+ | "Inside the method m1 by object of GFG class"); | ||
+ | |||
+ | // Calling m2() method within the same class. | ||
+ | this.m2(); | ||
+ | |||
+ | // Statements to be executed if any | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | // Method 3 | ||
+ | // Returns nothing | ||
+ | public void m2() | ||
+ | { | ||
+ | |||
+ | // Print statement | ||
+ | System.out.println( | ||
+ | "In method m2 came from method m1"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Class 2 | ||
+ | // Main class | ||
+ | class GFG { | ||
+ | |||
+ | // Main driver method | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | |||
+ | // Creating object of above class inside the class | ||
+ | Test obj = new Test(); | ||
+ | |||
+ | // Calling method 2 inside main() method | ||
+ | int i = obj.m1(); | ||
+ | |||
+ | // Display message only | ||
+ | System.out.println( | ||
+ | "Control returned after method m1 :" + i); | ||
+ | |||
+ | // Call m2() method | ||
+ | // obj.m2(); | ||
+ | int no_of_objects = Test.get(); | ||
+ | |||
+ | // Print statement | ||
+ | System.out.print( | ||
+ | "No of instances created till now : "); | ||
+ | |||
+ | System.out.println(no_of_objects); | ||
+ | } | ||
+ | } | ||
+ | |||
Output | Output | ||
− | Inside the method m1 by object of GFG class | + | * Inside the method m1 by object of GFG class |
− | In method m2 came from method m1 | + | * In method m2 came from method m1 |
− | Control returned after method m1 :1 | + | * Control returned after method m1 :1 |
− | No of instances created till now : 1 | + | * No of instances created till now : 1 |
− | |||
The control flow of the above program is as follows: | The control flow of the above program is as follows: | ||
Line 231: | Line 232: | ||
− | Memory Allocation for Methods Calls | + | ==Memory Allocation for Methods Calls== |
Revision as of 09:40, 3 May 2022
Method di Java atau Java Method adalah kumpulan perintah yang melakukan beberapa tugas tertentu dan mengembalikan hasilnya ke pemanggil. Method Java dapat melakukan beberapa tugas tertentu tanpa perlu memberikan return apa pun. Metdod di Java memungkinkan kita untuk menggunakan kembali code tanpa mengetik ulang code. Di Java, setiap method harus menjadi bagian dari Class yang berbeda dengan bahasa seperti C, C++, dan Python.
Catatan: Method adalah penghemat waktu dan penolong kita yang memungkinkan kita menggunakan ulang code tanpa menulis ulang code.
Method Declaration
Secara umum, deklarasi method ada enam (6) komponen:
- Modifier: Ini mendefinisikan jenis akses dari method yaitu dari mana method tersebut dapat diakses di aplikasi kita. Di Java, ada 4 jenis akses.
public: Itu dapat diakses di semua Class dalam aplikasi yang kita buat. protected: Itu dapat diakses di dalam Class di mana dia didefinisikan dan di subclass-nya. private: Itu hanya dapat diakses di dalam Class di mana ia didefinisikan. default: Itu dideklarasikan/didefinisikan tanpa menggunakan modifier apa pun. Itu dapat diakses di dalam class dan package yang sama di mana class didefinisikan.
- Return type: Tipe data dari nilai yang dikembalikan oleh method atau batal jika tidak mengembalikan (return) sebuah nilai.
- Method Name: aturan untuk nama field berlaku untuk nama method juga, tetapi konvensinya sedikit berbeda.
- Parameter list: Comma-separated list di definisikan, didahului dengan tipe datanya, di dalam kurung tertutup. Jika tidak ada parameter, kita harus menggunakan tanda kurung kosong ().
- Exception list: exception yang kita harapkan dapat di throw oleh method, kita dapat menentukan exception ini.
- Method body: di tutup antara dua kurung kurawal. Berisi code yang harus di jalankan untuk operasi yang kita inginkan..
Type Method di Java
Ada dua (2) type dari Method di Java:
- Predefined Method: Di Java, predefined method adalah metode yang sudah didefinisikan di library Class Java yang dikenal sebagai metdod yang telah ditentukan sebelumnya. Ini juga dikenal sebagai method library standar atau method build-in. Kita dapat langsung menggunakan method ini hanya dengan memanggilnya dalam program kapan saja.
- User-defined Method: Method yang ditulis oleh pengguna atau pemrogram dikenal sebagai user-defined method. Method ini dimodifikasi sesuai dengan kebutuhan.
Ciri Method
Ini terdiri dari nama method dan daftar parameter (jumlah parameter, type parameter, dan urutan parameter). Type return dan exception bukan bagian dari ciri method.
Contoh Ciri Method dari sebuah fungsi:
max(int x, int y)
Jumlah parameter adalah 2, type parameter adalah int.
Bagaimana Menamakan Method?
A method name is typically a single word that should be a verb in lowercase or multi-word, that begins with a verb in lowercase followed by an adjective, noun….. After the first word, the first letter of each word should be capitalized.
Nama metode biasanya satu kata yang harus menjadi kata kerja dalam huruf kecil atau multi-kata, yang dimulai dengan kata kerja dalam huruf kecil diikuti oleh kata sifat, kata benda….. Setelah kata pertama, huruf pertama dari setiap kata harus dikapitalisasi .
Rules to Name a Method
- While defining a method, remember that the method name must be a verb and start with a lowercase letter.
- If the method name has more than two words, the first name must be a verb followed by an adjective or noun.
- In the multi-word method name, the first letter of each word must be in uppercase except the first word. For example, findSum, computeMax, setX and getX.
Generally, a method has a unique name within the class in which it is defined but sometimes a method might have the same name as other method names within the same class as method overloading is allowed in Java.
Method Calling
The method needs to be called for using its functionality. There can be three situations when a method is called: A method returns to the code that invoked it when:
- It completes all the statements in the method
- It reaches a return statement
- Throws an exception
Contoh 1:
// Java Program to Illustrate Methods // Importing required classes import java.io.*; // Class 1 // Helper class class Addition { // Initially taking sum as 0 // as we have not started computation int sum = 0; // Method // To add two numbers public int addTwoInt(int a, int b) { // Adding two integer value sum = a + b; // Returning summation of two values return sum; } } // Class 2 // Helper class class GFG { // Main driver method public static void main(String[] args) { // Creating object of class 1 inside main() method Addition add = new Addition(); // Calling method of above class // to add two integer // using instance created int s = add.addTwoInt(1, 2); // Printing the sum of two numbers System.out.println("Sum of two integer values :" + s); } }
Output
- Sum of two integer values :3
Contoh 2:
// Java Program to Illustrate Method Calling // Via Different Ways of Calling a Method // Importing required classes import java.io.*; // Class 1 // Helper class class Test { public static int i = 0; // Constructor of class Test() { // Counts the number of the objects of the class i++; } // Method 1 // To access static members of the class and // and for getting total no of objects // of the same class created so far public static int get() { // statements to be executed.... return i; } // Method 2 // Instance method calling object directly // that is created inside another class 'GFG'. // Can also be called by object directly created in the // same class and from another method defined in the // same class and return integer value as return type is // int. public int m1() { // Display message only System.out.println( "Inside the method m1 by object of GFG class"); // Calling m2() method within the same class. this.m2(); // Statements to be executed if any return 1; } // Method 3 // Returns nothing public void m2() { // Print statement System.out.println( "In method m2 came from method m1"); } } // Class 2 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of above class inside the class Test obj = new Test(); // Calling method 2 inside main() method int i = obj.m1(); // Display message only System.out.println( "Control returned after method m1 :" + i); // Call m2() method // obj.m2(); int no_of_objects = Test.get(); // Print statement System.out.print( "No of instances created till now : "); System.out.println(no_of_objects); } }
Output
- Inside the method m1 by object of GFG class
- In method m2 came from method m1
- Control returned after method m1 :1
- No of instances created till now : 1
The control flow of the above program is as follows:
methods in java
Memory Allocation for Methods Calls
Methods calls are implemented through a stack. Whenever a method is called a stack frame is created within the stack area and after that, the arguments passed to and the local variables and value to be returned by this called method are stored in this stack frame and when execution of the called method is finished, the allocated stack frame would be deleted. There is a stack pointer register that tracks the top of the stack which is adjusted accordingly.
Panggilan metode diimplementasikan melalui tumpukan. Setiap kali suatu metode dipanggil, bingkai tumpukan dibuat di dalam area tumpukan dan setelah itu, argumen yang diteruskan ke dan variabel lokal serta nilai yang akan dikembalikan oleh metode yang dipanggil ini disimpan dalam bingkai tumpukan ini dan ketika eksekusi metode yang dipanggil selesai , bingkai tumpukan yang dialokasikan akan dihapus. Ada register penunjuk tumpukan yang melacak bagian atas tumpukan yang disesuaikan.