Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Importance of return type in Java?
A method in Java is a set of statements that can be reused in the program whenever we want. We can write a method once and call it multiple times.
- We can return values from a method; the datatype of the value we return is known as the return type.
- We return values from a Java method using a return statement. A return statement causes the program control to transfer back to the caller of a method.
- Every method in Java is declared with a return type, and it is mandatory for all Java methods to have a return type (at least void).
A return type may be a primitive type like int, float, double, a reference type, or a void type(returns nothing). There are a few important things to keep in mind while returning values from a method -
-
The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer.
-
The variable receiving the value returned by a method must also be compatible with the return type specified for the method.
-
The parameters can be passed in a sequence, and they must be accepted by the method in the same sequence.
Example: Returning Integer Value
The following program adds two integer values and returns their "sum" (i.e., is int type) as the result -
public class ReturnTypeTest1 {
public int add() { // without arguments
int x = 30;
int y = 70;
int z = x+y;
return z;
}
public static void main(String args[]) {
ReturnTypeTest1 test = new ReturnTypeTest1();
int add = test.add();
System.out.println("The sum of x and y is: " + add);
}
}
The above program produces the following output -
The sum of x and y is: 100
Example: Returning Floating point Value
This is another example demonstrating the usage of return types in Java. We define a function with parameters that returns the product of two numbers (which is of "float" type) of those integers as the result -
public class ReturnTypeTest2 {
public float prod(float x, float y) { // with arguments
float z = x*y;
return z;
}
public static void main(String args[]) {
ReturnTypeTest2 test = new ReturnTypeTest2();
float prod = test.prod(10.23f, 202.33f);
System.out.println("The product of x and y is: " + prod);
}
}
Following is the output of the above program -
The product of x and y is: 30
Example: Void Return Type
In the example below, we have used another return type called void. This return type is used when you don't want to return anything -
public class ReturnTypeTest2 {
public static void display(String name){
System.out.print("The name is: " + name);
}
public static void main(String[] args){
String name = "ABC";
display(name);
}
}
Output
Below is the output of the above program -
The name is: ABC
Note: If the method return type is void, then do not use a return statement with a value; otherwise, you will get an error.
