What are Methods | Advanced Java | Part – 1

what are methods advanced java

What are Methods?

Farming

The above picture consists of tasks related to farming.

The tasks such as sowing seeds, watering, fertilizing combine together to achieve a single goal which is good growth of crops.

And when all these processes work together we get the output in the form of green, nourished crops.

Similarly, Methods in programming are the group of statements written to carry out a specific operation.

You can think of methods as functions if you already know what they are.

This group of statements can be used multiple times once written.

Yup, write once use as many times as you want.

Java provides many built-in methods. Built-in methods are the ones which are present in the Java library and can be used directly.

Methods in programming are the group of statements written to carry out a specific operation.

Remember the System.out.println() method that we used?

It is a built-in method.

It is defined somewhere where a bunch of statements must be working together to perform the operation of printing items to the screen.

Can you imagine writing the same bunch of statements every time you want to print something to the screen?

Methods enable us to perform similar operations without writing the same statements repeatedly.

We can also write our own methods. Let’s explore how.

Writing a method

Methods in Java are written as described below

 public static int name(parameters){

//statements

}

public static are modifiers and are used to define the scope of methods. For now take them as granted.

int is the return type. It specifies the type of value returned by the method. It can be any of the supported data types. In this case, int means that the result of the execution of the functions is a whole number.

name is the name of method.

parameters are the data which we can pass to the method in order to perform operations.

For eg, if the method performs addition of two numbers then we need to pass the two numbers as parameters.

And statements are enclosed within curly brackets.

Let’s begin by writing a simple method.

 public static void hello(){

System.out.println(“Congratulations, you have written your first method.”);

}

That’s it we have written our first method in Java.

Noticed the void return type that we have used. It specifies that the method returns nothing.

This method takes in no parameters and prints “Congratulations, you have written your first method.” every time it is called.

So, now if we want to print something multiple times we don’t need to write System.out.println() again and again.

We are now able to define methods.

Very soon we are going to learn how to use/call methods.

Let’s write a method which takes in parameters and returns something.

ADDITON

We have to write a method which takes in two parameters, performs addition operation and returns the result.

Let’s break this down and start writing the method.

Consider we are performing addition of two integers, thus the return type will be int.

We need to pass two integers as parameters on which addition operation can be performed. So, our parameter list would look like (int num1, int num2) where num1 and num2 are the two integers respectively.

Now, what remains?

The statement to add these numbers and return their sum.

To return data from the method we use the return statement.

So, our final statement to add the two numbers and return the result would look like

 return num1 + num2;

Let’s combine these all to get our method in place.

 public static int sum(int num1, int num2){

return num1 + num2;

}

Awesome, we have written a method which takes in two integers and returns their sum as result.

So, now if we want to calculate the sum of any two integers, we will just call this method.

Calling a method

Calling a method

Enough of defining methods. Let’s see how we can use them.

To use the methods we have defined, we need to make a call to these methods.

Calling a method uses the following syntax:

 methodName(parameters);

That’s it. To call any method we only need to specify its name and its parameters in brackets, if any.

Now, can you guess what we have been doing since the beginning of time when we were writing System.out.println(“Isn’t this awesome?”);

We were actually making a call to the println method and passing a parameter that we wanted to print out to screen.

Let’s make a call to the methods we already defined.

class Main {

public static void hello(){

System.out.println(“Congratulations, you have written your first method.”);

}

public static void main(String[] args) {

hello();

//call to the hello method

}

}

The above will print Congratulationsyou have written your first method. on the output screen.

We can call the method as many times as we wish and it will print the text the same number of times.

class Main {

public static int sum(int num1, int num2){

return num1 + num2;

//returning sum of num1 and num2

}

public static void main(String[] args) {

int add = sum(2,4);

//call to the sum method

int add2 = sum(3,6);

//call to the sum method

System.out.println(add);

System.out.println(add2);

}

}

We are calling the sum method here and passing two numbers to perform addition operation. We called the method twice with different parameters.

The output of the above code:

6 9

To Summarize

  • Methods are reusable blocks of code written to perform specific tasks.
  • Methods in Java are written in the following way
  •  public static int name(parameters){
  • //statements
  • }
  • To call any method we need to specify its name and parameters in brackets, if any.

Leave a Reply

Your email address will not be published. Required fields are marked *