Return Value of the Java method (translated from Java tutorials)

Source: Internet
Author: User

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 operationBicycleObjects, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.