Während (wahr); Loop wirft unerreichbarem Code aus, wenn es nicht in einer Leere istJava

Java-Forum
Anonymous
 Während (wahr); Loop wirft unerreichbarem Code aus, wenn es nicht in einer Leere ist

Post by Anonymous »

Ich habe einige kleine Programme in Java gemacht. Ich weiß das, wenn ich schreibe

Code: Select all

while(true);
Das Programm friert in dieser Schleife [/b] ein. Wenn der Code so ist: < /p>

Test 1: < /strong> < /h2>

Code: Select all

public class While {
public static void main(String[] args) {
System.out.println("start");
while (true);
System.out.println("end");
}
}
< /code>

  Der Compiler wirft mir den Fehler aus: < /em> < /strong> < /p>

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable code
at While.main(While.java:6)
< /code>

Ich wusste nicht, dass dieser Fehler existiert. Aber ich habe bekommen, warum es geworfen wird. Natürlich war  Zeile 6 nicht erreichbar [/b] und verursachte ein Zusammenstellungsproblem. Dann habe ich dies getestet: < /p>

 [b] Test 2: < /strong> < /h2>

public class While {
public static void main(String[] args) {
System.out.println("start");
a();
b();
}
static void a() {
while(true);
}
static void b() {
System.out.println("end");
}
}
< /code>

Aus irgendeinem Grund  Das Programm lief normal < /strong> (die Konsole "Start" und dann eingefroren). Der Compiler konnte innerhalb von  nicht eincheckenvoid a()
[/b] und sehen Sie, dass es nicht erreichbar ist. Um sicherzugehen, dass ich es versucht habe: < /p>

Test 3: < /strong> < /h2>

Code: Select all

public class While {
public static void main(String[] args) {
System.out.println("start");
a();
System.out.println("end");
}
static void a() {
while(true);
}
}
< /code>

 Gleiches Ergebnis wie Test 2. Strong> Frage [/b]. , wenn der Code in den Klammern eine Variable ist, würde der Compiler die Ausnahme nicht auswerfen . Das macht Sinn, aber ich denke nicht, dass dasselbe für [b] giltvoids
[/b]. stark>

Code: Select all

void b()
(Test 2) und

Code: Select all

System.out.println("end");
[/b] (test 3) ist nicht erreichbar? p>

#include

using namespace std;

int main()
{
cout

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post