Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Methods and Functions

7. Methods and Functions

Methods, often referred to as functions in other programming languages, are blocks of code that perform specific tasks or computations. They are essential for organizing and reusing code in Java. In this Core Java tutorial, we'll delve into methods and functions, providing comprehensive explanations and examples.

Defining a Method:

In Java, a method consists of a method signature and a method body. The method signature defines the method's name, return type, and parameters (if any).

Syntax:
returnType methodName(parameter1Type parameter1Name, parameter2Type parameter2Name, ...) {
    // Method body
    // Code to perform the task
    return returnValue; // Optional, if the method has a return type
}

 

  • returnType: The data type of the value that the method returns. Use 'void' if the method doesn't return a value.
  • methodName: The name of the method.
  • parameter1Type, parameter2Type: The data types of the method's parameters, if any.
  • parameter1Name, parameter2Name: The names of the method's parameters.
  • returnValue: The value that the method returns, if the return type is not 'void'.

Example: A Simple Method:

Let's define a simple method that calculates the sum of two numbers and returns the result.

public int add(int num1, int num2) {
    int sum = num1 + num2;
    return sum;
}

 

In this example:

  • The method's name is 'add' .
  • It takes two parameters of type 'int' named  'num1' and 'num2'.
  • It calculates the sum of 'num1' and 'num2'and stores it in a variable called 'sum'.
  • Finally, it returns the value of 'sum'.

Calling a Method:

To use a method, you need to call it from another part of your program. To call a method, use its name followed by parentheses and pass any required arguments inside the parentheses.

Example:
int result = add(5, 3);
System.out.println("Sum: " + result); // Output: Sum: 8

 

In this example, we call the 'add' method with arguments '5' and '3', and it returns '8'. The result is stored in the 'result'variable, which is then printed to the console.

Method Overloading:

Method overloading allows you to define multiple methods with the same name but different parameter lists. Java determines which method to call based on the number and types of arguments passed.

Example:
public int add(int num1, int num2) {
    return num1 + num2;
}

public double add(double num1, double num2) {
    return num1 + num2;
}

 

In this example, we have two 'add' methods: one for  'int' arguments and another for  'double'arguments. Java will choose the appropriate method based on the argument types during the method call.

The 'void'Return Type:

If a method doesn't need to return a value, you can specify the 'void' return type. Such methods are used for performing actions without returning a result.

Example:
public void greet(String name) {
    System.out.println("Hello, " + name + "!");
}
 

In this example, the 'greet' method takes a 'String' parameter 'name' and prints a greeting message to the console.

Built-In Methods:

Java provides many built-in methods through libraries and classes. For example, the 'System.out.println()' method is used to print text to the console, and the  'String.length()' method returns the length of a string.

Example:
String greeting = "Hello, World!";
int length = greeting.length(); // Returns the length of the string
System.out.println("Length of the string: " + length);

 

Conclusion:

Methods are fundamental for structuring code, making it more modular and maintainable. They enable you to reuse code, enhance code readability, and separate concerns in your Java programs. Understanding how to define and call methods is a core skill for Java developers. In the next sections of this Core Java tutorial, we will explore more advanced topics and best practices related to methods and functions in Java.