return usage in Java. __java

Source: Internet
Author: User
The return statement in Java is always closely related to the method, which is always used in the method, has two functions, one is the value that returns the specified type of the method (this value is always determined), and one is the execution of the End method (only a return statement).
public void Test1 () {System.out.println ("---------no return Statement Test--------");              for (int i = 1;; i++) {if (i = = 4) return;          System.out.println ("i =" + i); }      }

When I=4, the loop ends.

Return has a feature that ends the method directly as soon as the return is invoked.

There are the following examples:?

public int Locate () {while (true) return 4; }

The above code is not an error,

?

public int Locate () {while (IsTrue ()) return 4;    Boolean isTrue () {return true; }

This code has an error, because the compiler believes that the while statement has the ability to execute in any case, but only if the argument is true. The JVM doesn't know what to do when istrue is fake, so it complains.

Looking at the following code:

?

public int Locate () {while (IsTrue ()) return 4;      return 0;            Boolean isTrue () {return true; }

This code also does not complain because it added return 0; Although the JVM does not know what to do when IsTrue is false, it still returns a value of 0, and the method returns a value.

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.