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 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, 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;
if ((num2 > num3) && (num2 > num1))
biggest = num2;
biggest = num3;
return biggest;

Output will be displayed as:

Download Source Code

Share on Google+Share on Google+

Java Method Return Value

Posted on: November 21, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Method Return Value   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
nadabo musa
February 19, 2013
object oriented programing

I am student your contribution encourage us thank
Raga Sandhya.Koyi
February 18, 2012

thank you code is useful for me.......but in this GetBiggestNumber() i wish to return mutiple values and this method is add to the vector and can i call this one in jsp ..........
Mahesh Dattatray Lanke
July 8, 2012

hi are how to call constroctor
chhote lal pal
September 20, 2012

this is good code resolution return type values print
October 29, 2012
Printing the return statement?

So I'm doing a project in which I need to make a table of values, one column of x and the other column of x! I know how to make the factorial program (and it worked before I put in the print statements), however when I'm trying to print the return statements, it's not working. Can someone please help me? public static double myexp (double n) { System.out.println(n); if (n<=1) { return 1; } else { System.out.print("\t"); //return n*myexp(n-1); System.out.print(return n*myexp(n-1)); } }
April 8, 2013

Code the following method headers only, each with inventoryData as its name: 1.Does not return or receive any values. 2.Receives the name of an inventory item. 3.Receives the price of an inventory item 4.Receives the price and quantity of an inventory item and returns an item total. 5.Receives an inventory item's integer-based item number and returns the name of the item.
Modiredi Williams Baratamang
August 13, 2013
Java concepts

I wanna learn the java concepts as a new java learning student.