Method in Java
- Eclipse: Oxygen
- Java: 1.8
In Java, Method is a block of code, which performs a specific task. It is also known as a function procedure.
“Methods” are blocks of code that can be used repeatedly by the programmer to reduce the complexity (length) of the code. There are various types of methods that are differentiated by the parameters passed or return type etc.
Syntax
Method declaration has six component
modifier returnType methodName (Parameters) {
// Method body
}
Modifiers – There are two types of modifiers in java. Access and Non-Access Modifiers
Access Modifiers are which control the scope of the defined function.
Access Modifiers are default, public, private and protected.
Non-Access Modifiers provide additional features.
There are final, static, transient, synchronized, volatile
The return Type – The return type for the method is always the returned data type and if the return type is void, then it returns nothing.
Method Name – It should be according to the naming convention decided by Java.
Parameters – parameters are for taking input from the user. You can provide a comma-delimited list of parameters, the parameter is optional, you can use empty parenthesis if there is no parameter.
Method body – It is always enclosed between braces “{ }” and contains the code for functionality.
Methods reduce the code size and increase the reusability of the code.
Example
The following code is an example of functionality print number from 1 to 10. Functionality is written inside a method named “loop”. To execute a method, it should be called in main”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Hello { public static void main(String[] args) { loop(); } public static void loop() { int i = 1; while (i <= 10) { System.out.println(i); i++; } } } |
Output
1
2
3
4
5
6
7
8
9
10
Parameterized Method
This is a way of passing value from the user. Parameterization is also used to define some variables inside the method block.
Using parameterization above example can be written as
- The variable step is responsible for the initial value of the loop.
- The variable ‘final_value’ is responsible for the final value up to which the loop will execute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Hello { public static void main(String[] args) { loop(1, 10); } public static void loop(int step, int finalvalue) { while (step <= finalvalue) { System.out.println(step); step++; } } } |
Output
1
2
3
4
5
6
7
8
9
10
Method Parameters & Return Type
Here we will deal with parameters and return type of the method. Return type parameter which could be a value, object or any data structure is returned by method after the completion of its execution. The “return” keyword is used to return a value from a method.
- A method can return primitive or non-primitive type value.
- A method can return only a single It may or may not contain multiple values.
The “return” keyword ideally is the last statement of the method but it could also feature anywhere in the given code depending upon the code. Once the return statement is executed, the program returns to the code from where the method is called. Code written after the return statement will not be executed, it will give you a compile-time error.
Example
The method with parameter and without return type.
In this example method is returning nothing, we are displaying the area in the console with the help of print statement inside the method.
1 2 3 4 5 6 7 8 9 10 11 |
public class Hello { public static void main(String[] args) { System.out.println("Area of Rectangle with length =5.4 " + "and width = 3.2 "); areaRectangle(5.4, 3.2); } public static void areaRectangle(double length, double width) { System.out.println(length * width); } } |
Output:
Area of Rectangle with length =5.4 and width = 3.2
17.28
Example
A method with parameter and return type.
In this example method returns the area of a rectangle and the returned value is stored in the variable “area” outside the method.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Hello { public static void main(String[] args) { System.out.println("Area of Rectangle with length =5.4 " + "and width = 3.2 "); double area = areaRectangle(5.4, 3.2); System.out.println(area); } public static double areaRectangle(double length, double width) { return (length * width); } } |
Output:
Area of Rectangle with length =5.4 and width = 3.2
17.28
Method Overloading
In Java, Method overloading allows having multiple methods with the same name, if they differ in the number of arguments.
Method overloading can be achieved in the following ways:
- Differs in numbers of parameter
- Differs in the data type of parameters
- Differs in the order of parameters.
Example
- Method Overloading with different number of parameter
In the following example, we have two methods with the same name “area” but with a different number of parameters.
First “area” method accepts two arguments and calculating the area of a rectangle.
Second “area” method accepts a single argument and calculating the area of a square.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Hello { public static void main(String[] args) { System.out.println("Area of rectangle " + area(14.25d, 10.65d)); System.out.println("Area of Square " + area(5.0d)); } public static double area(double length, double width) { return (length * width); } public static double area(double side) { return (side * side); } } |
Output:
Area of rectangle 151.76250000000002
Area of Square 25.0
2. Method overloading with a different data type of parameter
In the following example, we have two methods with the same name “sum” but with different data types of parameters.
First “sum” method accepts two integer arguments and calculating the sum of integer values and return an integer value.
Second “sum” method accepts one integer and one float argument and calculating the sum of an integer and a float value and returns a float value.
Third “sum” method accepts two double arguments and calculating the sum of double values and return a double value.
Note: Method overloading cannot be achieved by changing the return type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class Hello { public static void main(String[] args) { System.out.println("1+2=" + sum(1, 2)); System.out.println("1+2.5=" + sum(1, 2.5f)); System.out.println("1.3+2.3=" + sum(1.3, 2.3)); } public static int sum(int x, int y) { System.out.println("Adding two integer entities"); return x + y; } public static float sum(int x, float y) { System.out.println("Adding one integer and one float entity"); return x + y; } public static double sum(double x, double y) { System.out.println("Adding two double entities"); return x + y; } } |
Output
Adding two integer entities
1+2=3
Adding one integer and one float entity
1+2.5=3.5
Adding two double entities
1.3+2.3=3.5999999999999996
3. Method overloading with the difference in order of parameters.
In the following example, we have the difference in order of parameters which the methods of the same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Class One{ public void orderParameters(int id,String name){ System.out.println(“Id:”+id+” Name: “+name); } public void orderParameters(String name,int id){ System.out.println(“Name: “+ name + “Id :“+id); } } Class Two{ public static void main(String[] args){ One o=new One(); o.orderParameters(“Ajay”,10); o.orderParameters(10, “Ajay”); } } |
Output:-
Name : Ajay Id: 10
Id : 10 Name : Ajay
Contributed by: Poonam Tomar