From http://www.cnblogs.com/ggjucheng/archive/2012/11/29/2795468.html
CodeWhen a method is called, it returns the following results:
Complete all statements of the Method
Return Statement appears
Throw an exception
Take the first appearance as the standard.
In the method declaration, the return type of the method is declared. Method body, you can use the return statement to return values.
Any method that declares void does not require a return value. It does not have to include a return statement, but it can also do so. In this case, the return statement can be used to exit the code block of the control flow and exit the method. The simple usage is as follows:
Return;
In a void declaration method, a value is returned, and the compiler reports an error.
It is not a method declared as void. It must contain a return statement followed by the corresponding return value, just like this:
ReturnReturnvalue;
The returned data type must match the returned data type declared by the method. A boolean value cannot be returned if the returned data type is an integer.
The rectangleGetarea ()
Method returns an integer:
//A method for computing the area of the rectanglePublic IntGetarea (){ReturnWidth *Height ;}
Expression returned by this methodThe integer calculated by width * height.
The getarea method returns the native type. Method can also be replaced with the reference type. For exampleProgramModerate operationBicycle
Objects, we may do this:
PublicBicycle seewhosfastest (bicycle mybike, bicycle yourbike, Environment env) {bicycle fastest;//Code to calculate which bike is//Faster, given each bike's gear//And cadence and given//Environment (terrain and wind)ReturnFastest ;}
Return class or interface
If this section is confusing, skip it and wait until you have completed the interface and inheritance learning before looking back at this.
If a method uses a class name as the return type, for exampleThe whosfastest method indicates the type of the class of the returned object, which must be a subclass of the declared return type or a declared return type. Suppose there is such a class hierarchy,Imaginarynumber is a subclass of number and number is a subclass of object, as shown in:
Imaginarynumber class hierarchy
If there is such a method that returns Number:
PublicNumber returnanumber (){...}
The returnanumber method can returnImaginarynumber, but cannot return an object.Imaginarynumber is a number class because it isNumber subclass. However, an object class may not be a number-it can be a string or another type.
PublicImaginarynumber returnanumber (){...}
This technique is called the covariant return type. The return type can be multiple subclasses but the same branch.
Note: You can also use an interface as the return type. In this case, the returned object must implement this interface.