3.2 KiB
Methods
What are methods
A java method can be interpreted as a subprogram. It is a collection of statements taht are grouped togehter to perform an operation
Built- in vs User-defined Methods
Built-in:
Built-in methods are part of the complier package such as
System.out.println
Function (return) - Type: it calcualtes and return a value
public static int calculate(int number){
return 1;
}
Procedure-type: executes some commands
Function (return)- type:
public static return type method-name (parameter 1)
Procedure-type method:
public static void method-name (paramenter 1)
How to create a method
In general, method declarations has 5 basic components: - Modifier: defines access type of the method i.e. from were it can be accessed in your application (For example: public) - The return type: the data type of the value returned by the method or void if it does not return a value (procedure and function type) - Method name: a specific names that identifies the method that can be used to invoke it later - Parameter list: Comma separated list of the input parameter are defined preceded with their data type, within the enclosed parentheses, If there are no parameters, you must use empty parentheses() - Method body: It is enclosed between braces. the code that you need to be execute to perfrom your intended operations
public int max(int x, int y) {
if(x > y) {
return x;
}
return y;
}
How to call a method (method invocation
To invoke a method you need the method name with the parameter list defined between parentheses
Exammple:
Method Name (paramter list)
Must have () parentheses when calling a method, even with no passing parameters
Pass-By-Value
What happens: When a method is called, a copy of the value of each argument is passed to the method
In the second method: This copy can be changed inside the method, however such a change has no effect on the actual argumnet
int num=10;
double decimal = 5.2;
NumberManeuvers(num decimal);
System.out.println("num = " + num + "and decimal = " + decimal);
public static void numberManeuvers(int i, double j) {
if(i == 10) {
= 6.2;
j = 12;
i }
}
// output: num = 10 and decimal = 5.2
Pass-By-Reference
What happens: When an object (Array, String) is passed to a method, its memory location address (rreference point) is used
The object: Arrays & trings behave like objects
In the second method: When their memory location is passed to the method the oject can be manipulated in the method resulting in actual changes to the object (Array, String)
int [] nums = {1, 2, 3};
testingArray(num);
System.out.println("num[0] = " + num[0] + ", num[1] = " + num[1] + ", num[2] = " + num[2]);
public static void testingArray(int[] value) {
value[0] = 4;
value[1] = 5;
value[2] = 6;
}
}
Benefits to methods
There are many advantages of using methods. Some of them are listed below: - It makes the program well structured - methods enhance the readability of the code. - It provides an effective way for the user to reuse the existing code. - Allows for easier debugging.