"Java" Unreachable Code compilation exception

Source: Internet
Author: User

The following code is not compiled in Java:

public class Unreachabletest {public static void main (string[] args) {while (true) {}system.out.println ("aaaaaa");}}

This code does not seem to have any syntax errors, but it is very obvious that this code is not meant to be, AAAAA will never output,

Java, however, differs from the C language in that Java considers this code to be an unreachable language error and cannot be compiled at all.

If you run this code, the following conditions will occur,


This is not to say that the structure of your while (true) is wrong, or that there is no code after the while (true), and that there is no break in the structure, and there is no code behind it, such as:

public class Unreachabletest {public static void main (string[] args) {when (true) {break;} System.out.println ("aaaaaa");}}
Is the correct code, running the result is aaaaaa, or is also the correct code, although this program never end:

public class Unreachabletest {public static void main (string[] args) {while (true) {}}}
So, when you write a program that needs to use a dead loop structure, if Eclipse makes that kind of simplified, saying that you're wrong about the code behind the while, you should understand what's going on, rather than just follow the advice and delete it.


"Java" Unreachable Code compilation exception

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.