Java Method Return Value

The return statement is used to return the value within the body of method. The method declared void does not return any value.

Java Method Return Value

The return statement is used to return the value within the body of method. The method declared void does not return any value.

Java Method Return Value

Java Method Return Value

     

Java  method Return Value return to the code when it -

  1. When  it completes all the statements in the method
  2. When it  reaches a return statement
  3. throwing  an exception, whichever occurred first.

The return statement is used to return the value within the body of method. The method declared void does not return any value. In case you try to get the return value from void .the code show you a compiler error. 

The method that is not declared void must contain a return statement with the  corresponding return value.:

Understand with Example

The Tutorial illustrates a code that help you in understanding JAVA method Return Value. In this Program we have a class Return Value, Inside the class we have a main method .The code show you how to get biggest number, for this we define a public static int GetBiggestNumber ( ) accept a int num1,num2,num3 as parameter. The if conditional operator evaluate and compare the numbers and return the biggest number. Finally the println print the get Biggest Number  is 20 from 10,15,20.in the command prompt.

Here is the code:

class ReturnValue
{
public static void main (String[] args)
{
System.out.println("The Biggest Number is: "+GetBiggestNumber(10, 15, 20));
}
public static int GetBiggestNumber (int num1, int num2, int num3)
{
int biggest = 0;
if ((num1 > num2) && (num1 > num3))
biggest = num1;
else
if ((num2 > num3) && (num2 > num1))
biggest = num2;
else
biggest = num3;
return biggest;
}
}

Output will be displayed as:

Download Source Code