Java Learning Journey-8

Source: Internet
Author: User
The use of break in Java, like in C, is used to end the loop, however, what surprised me is the use of return-the end of the current Method!!! Seeing is believing, let's take a look at the code:

int i;        for (i = 0;i < 8 ;i ++ )        {            if(i == 7) break;            else System.out.println(i);        }        System.out.println("ending******");

The results are as follows:

for (i = 0;i < 8 ;i ++ )        {            if(i == 7) return;            else System.out.println(i);        }        System.out.println("ending******");

The results are as follows:

Compare to discover:
There is no output ending***** when using return, why is this?
Break is the end of the current loop, so the current loop finishes and outputs ending***** before terminating the loop. The function of return is to terminate the current method (this is the main method), so the method terminates immediately after the return execution, so the program that outputs ending****** in the current method will no longer execute.

Java Learning Journey-8

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.