Summary of usage of return statements in Java

Source: Internet
Author: User

Return is the main language of the regulars, basically have a return statement, then in Java, return what is the special place, let's analyze the next

1, the function of the return statement: A, return a value, this value can be any type. B. Return the program to the operating system (that is, terminate the program)

2, in Java for a function, whether there is no return value type, you can have a return statement. The difference, however, is whether the return statement can return a value (depending on the return value type of the function).

A, if the function has a return value type (that is, the return value type is not void), you must have a return statement that returns the corresponding type value.

b, if the function has no return value (that is, the return value type is void), the return statement in the function, if there is a return statement! ) cannot be added to any of the following variables.

(The function in this case can also have no return statement, but if so, return can only be used as a function of returning the operating system.) )

For example:

1, there is a return value

public int Getage ()
{
return age; Returns the value of the variable age of type int

}

2, no return value//function without return statement

public void Putage ()
{
System.out.println (age);

}

3, return to the operating system//function no return value, but with a return statement

public void put (int a)

{

if (a > 0)

Return The return statement is not returned with a value, the function is to exit the program's Run

Else

System.out.println ("FASFSA");

}

Summary of usage of return statements in Java

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.