MY mENU


Thursday 9 February 2012

Return Statement in Java


The last of the Branching Statements is the return statement. The return statement exits from the current method, and control flow returns to where the method was invoked.

The return statement has two forms :

1. Returns a value
2. Doesn't returns a value

Returns a value : To return a value, simply put the value (or an expression that calculates the value) after the return keywordThe data type of the returned value must match the type of the method's declared return value.

return ++count;

Doesn't returns a value : When a method is declared void, use the form of return that doesn't return a value.

return;

eg:


 int Java(int x,int y)
{f
int c=x+y;
return c;
}
In the above Eg: the method Java is with a int datatype.so it must contain a return statement. i.e it returns a integer value.

Eg: void Java()
{
System.out.println("This is java");
}

No comments:

Post a Comment