In Java, the method return type is the value returned before a method completes its execution and exits. Let's see some of the most critical points to keep in mind about returning a value from a method.
Returning anything from a method with a void return type leads to a compile error.
A method with a void return types means this method cannot return a value. But, if you are still returning a value from a method with a void return type, a compile error will be thrown.
class A
{
public void message()
{
System.out.println("Hello");
return 1; //Returning an int value from a method with void return type.
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}
An empty return statement in a method with a void return type works fine.
An empty return statement is acceptable within a method with a void return type because it doesn't return any value.
class A
{
public void message()
{
System.out.println("Hello");
return; // empty return statement is perfectly fine in a void method
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}
Output-
Hello
A larger primitive return type of a method can be used to return a smaller primitive value.
A method with larger primitive return type can return any smaller primitive type values, as long as it doesn't lead to a loss in the value of smaller primitive types.
class A
{
byte b= 10;
public int returnValue()
{
System.out.println("Hello");
return b;
}
public static void main(String... ar)
{
A ob= new A();
System.out.println(ob.returnValue());
}
}
Output-
10
Here we are returning a byte value out of a method that has an int return type, this works because -
An int data type is larger than byte, hence int return type is large enough to hold a byte value and return it from the method.
There is no loss in the value of byte even if its value is widened to an int.
Advertisement
A char value is returned from a method with double return type
class A
{
public double returnValue()
{
char c ='a';
return c;
}
public static void main(String... ar)
{
A ob = new A();
System.out.println(ob.returnValue());
}
}
Output-
97.0
Here we are returning a char value out of a method that has a double return type, this works because -
Data type double is larger than char, hence, double return type is large enough to hold a char value and return it from the method.
There is no loss in the value of char even if its value is widened to a double.
A floating-point value cannot be returned from a method with an integer return type.
In the upcoming code, we are going to return a float value from a method with a long return type.
class A
{
public long returnValue()
{
float f = 10.5f;
return f;
}
public static void main(String... ar)
{
A ob = new A();
System.out.println(ob.returnValue());
}
}
Output-
Return10.java:8: error: incompatible types: possible lossy conversion from float
to long
return f;
^
1 error
To return a float out of a method with a long return type, the float value must be converted to long. When converting a float value to long,
the decimal value of a float will be truncated, leading to a loss in the value of float, which is not allowed by the compiler, hence, a compile error.
The type of a superclass can be used to return an object of its subclass from a method.
In this example, class A is extended by class B.
The method message() in class B has a return type of its superclass, A.
From this method, we have returned the currently executing object of class B, using this reference.
class A
{
}
class B extends A
{
public A message()
{
System.out.println("Hello");
return this; //currently executing object of B type is returned from a method with superclass return type.
}
public static void main(String... ar)
{
B ob = new B(); // an object of B is created
ob.message(); //method message() is called on this object of B
}
}
Output-
Hello
Returning an array from a method with the Object class as its return type
In Java, an object of any type(array object or a class object) has a parent class, Object i.e. mother of all classes in Java.
Hence, an array or an object of any class can be returned from a method with the Object return type.
class A
{
public Object message() //Method with Object class return type
{
int arr[] = new int[]{1,2,3,4};
return arr; //returning an int array, which is also an object.
}
public static void main(String... ar)
{
A ob = new A();
int arr2[]=(int[])ob.message(); //casting the Object reference back to int array
for(int i=0;i<arr2.length; i++) //Reading the int array
System.out.println(arr2[i]);
}
}
Output-
1
2
3
4
Returning a null reference from a method with a class return type
A null reference means a reference which is currently not pointing to any object on the Heap but it has the capability to point to an object in the future.
Hence, a null reference can be returned from a method with a return type of any class.
class A
{
public A message()
{
System.out.println("Hello");
return null;
}
public static void main(String... ar)
{
A ob = new A();
ob.message();
}
}