06.01. Methods in Java

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”

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.

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.

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.

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:

  1. Differs in numbers of parameter
  2. Differs in the data type of parameters
  3. Differs in the order of parameters.

Example

  1. 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.

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.

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.

Output:-

Name : Ajay Id: 10

Id : 10 Name : Ajay

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments