CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Give me clear Idea about return statement (http://www.codingforums.com/showthread.php?t=207883)

Anep 10-27-2010 08:04 AM

Give me clear Idea about return statement
 
Dear All,

I am confusing little bit on return statements. Give me clarity about the return statements.
Explain with Example.

sujithpr 10-30-2010 07:37 AM

The return statement is used to return the value within the body of method. The method declared void does not return any value.
The method that is not declared void must contain a return statement with the corresponding return value.:

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:

The Biggest Number is :20

:)

Dmor574 11-10-2010 08:44 PM

In simple terms, if you call a method that returns a value(which looks like this)

Code:

public int number() 
{
 int x = 5;
 return x;
}

and this method above returns an 'int' type. A method that returns a value must use the keyword: "return". and whatever follows the return statement must correspond with what you wrote in the method declaration.

This example says 'int' in the declaration, but you can change it to any primitive, or any object, including boolean, String, etc.

If you would call that method, whatever is returned (in this case x = 5, which is 5) is replaced by the call to the method.

Code:

int y = number();
is like saying

Code:

int y = 5;


All times are GMT +1. The time now is 05:58 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.