Method Return Types and Parameters in Java

(Last Updated On: February 24, 2019)

Learn what is method return types and parameters in java with code example and simple explanations.

You will be learning…

  • how to return int and string data types in java from a method.
  • Using void keyword to prevent returning data from a method.
  • methods parameters and how to call them by supplying the values known as arguments.

 

Method Return Types in Java

We have learned what is method in java with Syntax and definition already in previous post and have learned basics about it. Now, lets learn about return type of a method in java.

Syntax of method in Java

 

 

Return type: void – The method returns nothing

We use “void” keyword if we want a method not to return anything but perform operations only / Execute group of statements.

NOTE: if return type is anything except void, then method must have “return “statement.

 

Return type: int – The method returns int data type

NOTE: If you have return type “void”, then you don’t need to write “return” statement. As we have written return statement e.g. return 2+3; in above method that is returning int data type of value.

 

Return type: String – The method returns String data type of value

 

 

Java Code Example:

Lets see how we can call a method returning int value and use them. As an example, lets call the method myMethod() in main()method of java program.

In main method, we have call the myMethod() that returns value 5. So, have created a int variable to store that value and display that using system.out.println method.

 

 

DOUBT: Can’t we call the method directly in system.out.println. why to create a local variable of int type in main() method?

Answer: Yes, we can call the method directly in system.out.println method as below, if we want to just check if myMethod() is returning correct value or not.

System.out.println(“Value returned from method myMethod()= ” + myMethod());

 

But if you want to use the value returned by myMethod()further in the main() method, then you need to store that in a variable and use it further.

OK, so for we understand what return type of method in java and how to call them and store their value.

 

Let’s see one more example of returning value from a method. Below example, method myMethod() returns a String value and it is called from main() method and display the returned value.

Output:
My name is :Rakesh Singh

 

NOTE: methods in java must have a return type. if not returning use return type “void”

 

Method Parameters in Java

If you look at the syntax of method, we have learned return type. Now, we will learn about method parameters in java i.e. methodName(list of parameters).

Syntax of method

A method receives value via parameter from where the method is called. It can have one or more parameters.
In below examples, the add method takes two int type of parameter i.e. int first and int second.
print method is taking one parameter of String type.

 

How to call methods with arguments in Java?

Let’s see a complete simple example.

In this example, we have an add method with two int type parameters i.e. int add(int first, int second), that will calculate sum using both parameters first and second and return the sum.

In main() method, we are calling add method by supplying two int values, also, known as agreements. method add, will receives these two arguments in its 2 parameters first and second and return the sum to main() method.

 

 

 

Method Parameter Vs Method Arguments in java

In method declaration e.g. add(int first, int second), variable first and second are known as method parameter list that we write them during declaration of a method.

When we call a method by supplying values e.g. int result = add(10, 20); in above program, then these values are known as method arguments.

 

Exercises on Method Return Types and Parameters in Java

Exercise-1: Create a method named “print”. The method have 1 parameter of String type. It does not return anything. Call print method from main() method with string value and Display the message inside print method.

Solution to Exercise-1: