Method Return Types and Parameters in Java

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

modifier returnValueType methodName(list of parameters) {
			 // Method body;
			//Group of statements
			}

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.

public static void myMethod() {
		
//no return statement as return type of method is void
	}

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

Return type: int – The method returns int data type

public static int myMethod() {
		
		//return int value 5
		return 2+3;
	}

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

public static String myMethod() {
		
		//return String value Rakesh Singh
		return "Rakesh Singh";
	}

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.

public class Sample {

	public static void main(String[] args) {

		int value = myMethod();
		System.out.println("Value returned from method myMethod()= " + value);

	}

	public static int myMethod() {

		// return int value 5
		return 2 + 3;
	}

}

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.

public class JavaMethods {

	public static void main(String[] args) {

		//call the method. the method will return
// string value
		//Store that in a variable of string type
		String name = myMethod();
		
		System.out.println("My name is :" + name);
	}
	
	public static String myMethod() {
		
		String name = "Rakesh Singh";
		return name;
	}	
	
}

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

modifier returnValueType methodName(list of parameters) {
			 // Method body;
			//Group of statements
			}

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.

public static int add(int first, int second){}
public static int print(String msg) {}

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.

public class Sample {

	public static void main(String[] args) {

		// call the method add by supplying value
		int result = add(10, 20);
		
		System.out.println("Sum returned =" + result);
	}

	public static int add(int first, int second) {
		// use parameter to find sum
		int sum = first + second;

		return sum;// return the sum
	}

}

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:

public class Sample {

	public static void main(String[] args) {

		print("Hello World");

	}

	public static void print(String msg) {

		System.out.println(msg);
	}

}

Related Posts