Java Methods [with Examples]

Java Tutorials

Summary: In this tutorial, you will learn what is the use of methods in Java, how can you create one and use it in your Java program.

Method in Java

In Java, a method is a block of code that performs a specific task. It is a way to reuse code and can be called multiple times from different places in a program.

To create a method in Java, you must define the method’s name, return type, and parameters. Here is the general syntax for defining a method in Java:

modifier returnType methodName(parameter1Type parameter1Name, parameter2Type parameter2Name, ...) {
  // method body
}

Here is an example of a simple method in Java that takes a single int parameter and returns an int value:

public static int addOne(int x) {
  return x + 1;
}

This method has a return type of int, a name of addOne, and a single parameter of type int named x. The method’s body consists of a single statement that returns the value of x plus 1.

Here is another example of a method that takes no parameters and has a return type of void:

public static void printHello() {
  System.out.println("Hello, World!");
}

This method has a return type of void, a name of printHello, and no parameters. The method’s body consists of a single statement that prints a message to the console.

To call a method in Java, you use the method’s name followed by a set of parentheses. For example:

printHello();
int y = addOne(5);

The first line calls the printHello method, which prints “Hello, World!” to the console. The second line calls the addOne method, which returns the value 6. The returned value is then stored in the y variable.

Parameters

A parameter is a value that is passed to a method when it is called. The method can then use the parameter to customize its behavior. For example, here is a method that takes a single parameter:

public static void printMessage(String message) {
  System.out.println(message);
}

This method has a single parameter of type String. When the method is called, the caller must provide a String value as an argument. The method can then use this value to print the message to the console.

You can also have multiple parameters in a method. For example:

public static void printSum(int a, int b) {
  System.out.println(a + b);
}

This method takes two int values as parameters and prints their sum to the console.

Parameter Types

A parameter type is the data type of a parameter that is passed to a method. In the examples above, the parameter types were String and int.

Java has a wide range of built-in data types, including primitive types (such as int, float, and boolean) and reference types (such as String and Object). You can also define your own custom data types by creating classes.

When you define a method, you must specify the data type for each of its parameters. This helps the Java compiler ensure that the correct type of value is passed to the method when it is called.

Return Types

A return type is the data type of the value that a method returns to the caller. For example, here is a method that returns a value of type int:

public static int add(int a, int b) {
  return a + b;
}

This method takes two int values as parameters and returns their sum. The return type of the method is int, so the method must return an int value.

If a method does not return a value, its return type is void. For example:

public static void printMessage(String message) {
  System.out.println(message);
}

This method has a return type of void, which means it does not return a value.

Advantages of Methods in Java

There are several advantages to using methods in Java:

1. Code Reuse

One of the biggest advantages of using methods is that they allow you to reuse code. Instead of writing the same code multiple times in your program, you can define it once in a method and call the method whenever you need to execute the code. This can make your code easier to read and maintain.

For example, consider the following program that calculates the average of three numbers:

public class Main {
  public static void main(String[] args) {
    int a = 3;
    int b = 4;
    int c = 5;
    double average = (a + b + c) / 3.0;
    System.out.println("The average is: " + average);
  }
}

We can use a method to calculate the average of three numbers as follows:

public class Main {
  public static void main(String[] args) {
    int a = 3;
    int b = 4;
    int c = 5;
    double average = calculateAverage(a, b, c);
    System.out.println("The average is: " + average);
  }

  public static double calculateAverage(int a, int b, int c) {
    return (a + b + c) / 3.0;
  }
}

In this example, the calculateAverage method is defined to take three int values as parameters and return their average as a double.

The main method then calls the calculateAverage method and passes it the values of a, b, and c. The returned value is then stored in the average variable and printed to the console.

2. Modularity

Methods allow you to divide your code into logical blocks, which can make your code easier to understand and maintain.

For example, you might define a separate method for each task that your program performs. This can make it easier to see how the different parts of your program fit together and make it easier to locate and fix problems when they arise.

3. Improved Readability

By breaking your code into smaller, more focused blocks, methods can make your code easier to read and understand. This can be especially helpful when working with large or complex programs.

4. Method Overloading

Java allows you to define multiple methods with the same name, as long as they have different parameter lists. This is known as method overloading.

Method overloading can be useful when you want to provide different behaviors for a method depending on the context in which it is called. (You will study more about in later tutorials).


Leave a Reply

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