Infinite loops in Java

In this post, we will see ways to create a infinite loop in Java

Creating an infinite loops endlessly might be an programming error but sometimes it will be intentional to support the behavior of your application

Using do..while

public void loopDoWhile() {
    do {
        // do something
    } while (true);
}

Using infinite for

public void loopInfiniteFor() {
    for (;;) {
        // do something
    }
}

Using While

public void loopWhile() {
    while (true) {
        // do something
    }
}


Also published on Medium.

Default image
Vignesh A Sathiyanantham
AWS Certified developer with 8 years of extensive experience working for Cloud and Datacenter solutions and leading a small team in a lean startup based on Bangalore, India
Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.