All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Return Value From Method In Java

Last Updated : Mar 11, 2024

How To Get Return Value From Method In Java

In this article we will show you the solution of how to get return value from method in java, Java's return keyword is probably one of the most underused.

A return value can be specified for almost every programming language and allows a programmer to specify the current method's result.

Java's return keyword returns a value after a method has been called. When the keyword is encountered, the method immediately returns the value.

In other words, all statements in the method will be discarded after the return keyword is used, and any local variables created within it will be discarded as well.

Regardless of whether a value is included with the return keyword, developers can use it.

In this case, the caller will receive the specified value back. If null is returned, it will be returned as a null value.

We will now discuss the idea of how to get return value from a method in java with an example.

Step By Step Guide On How To Get Return Value From Method In Java :-

Code 1

import java.util.Scanner;
public class talkerscode
{
    void input()
    {
        int x, y, z;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter first integer:");
        x = s.nextInt();
        System.out.print("Enter second integer:");
        y = s.nextInt();
        z = add(x, y);
        System.out.println("Result:"+z);
    }
    int add(int a, int b)
    {
        int c;
        c = a + b;
        return c;
    }
    public static void main(String[] args)
    {
        Return_Value obj = new Return_Value();
        obj.input();
    }
}

Code 2

import java.util.Scanner;
public class talkerscode
{
   public static void main(String[] args)
   {
      Scanner console = new Scanner(System.in);
      int number,
              x;
      System.out.print("Enter number : ");
      number = console.nextInt();
      x = factorial(number);
      System.out.println("Factorial : " + x);
   }
   public static int factorial(int num)
   {
      int fact = 1;
      for (int i = 1; i <= num; i++)
      {
         fact = fact * i;
      }
      return fact;
   }
}
  1. The program starts by importing the Scanner class which allows user input to be taken from the console.
  2. The class "talkerscode" is defined which contains two methods: "input" and "add".
  3. The "input" method takes user input for two integers and stores them in variables x and y.
  4. It then calls the "add" method with the two integers as parameters and stores the result in variable z.
  5. The "add" method takes two integers as parameters, adds them together and returns the result.
  6. The main method creates an object of the "talkerscode" class and calls the "input" method on it, which will execute the code and take user input.
  7. The program will output the result of the addition using the value stored in variable z.

Conclusion :-

As a result, we have successfully learned how to get return value from a method in java with an example. Functions can have parameters and return values.

A return value is a result of the function's execution. It can be returned to the block of code that called the function and then used as needed.

Parameters are the input for a function that are necessary for it to be executed and produce a result. Parameters are variables defined by name and type.

They are specified in the function's declaration. When calling a function, you are passing values to those variables. Those values are called arguments.

I hope this article on how to get return value from method in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪