Infinite Loop in Java

Infinite Loop in Java

by Ignis Dei
27 views

In this article, we’ll talk about the Java Infinite Loop and how you can take full advantage, through detailed examples.

1. What is an Infinite Loop?

This loop is an instruction sequence that loops endlessly when a terminating condition has not been set, cannot occur, or causes it to restart before it finally ends. These loops are also known as endless loops.

2. Use of Infinite Loops

Usually, these loops are errors that were created by programmers but there are a few situations where we use them for some logic that should be running periodically. These loops will exit only on the termination of the application.

Now let’s see different kinds of loops in which we will create some infinite loops.

2. While Infinite Loop

At first, let’s start with the while loop. Here we’ll use the boolean literal true to write the while loop condition. Let’s see the example:

public class WhileLoop {
    public static void main(String []args){
        while (true) {
            System.out.println("The loop is running..");
        }
    }
}

The output is:

The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
....
....

3. For Infinite Loop

 As for the for loop, we use the boolean value as true in the condition. Let’s see an example:

public class ForLoop {
    public static void main(String []args){
        for (;;) {

            System.out.println("The loop is running..");
        }
    }
}

The output is:

The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
....
....

4. Do-while Infinite Loop

These loops in Do-while loops very rarely to be used in java because the first while block will be executed and then the condition is checked. Let’s see an example:

public class DoWhileLoop {
    public static void main(String []args){
        do  {
            System.out.println("The loop is running..");
        } while(true);
    }
}

The output is:

The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
The loop is running..
....
....

5. Memory Error

It is obvious that when we run an infinite loop we need to use memory in order to store the process of the code which is inside the loop but as expected, memory is not unlimited. As a result, the compiler will throw a java.lang.OutOfMemoryError. The Java Garbage Collector cannot free up the space required for a new object, which causes a java.lang.OutOfMemoryError. This error can also be thrown when the native memory is insufficient to support the loading of a Java class.

Here we can see an example in which we add unlimited objects inside an Arraylist because of an infinite While loop:

public class MemoryError {

        public static void main(String args[])
        {
            ArrayList<Object> list = new ArrayList<Object>();

            while(true)
            {
                Object obj = new Object();
                list.add(obj);
            }
        }
    }

The output is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.util.Arrays.copyOf(Arrays.java:3210)
	at java.util.Arrays.copyOf(Arrays.java:3181)
	at java.util.ArrayList.grow(ArrayList.java:265)
	at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
	at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
	at java.util.ArrayList.add(ArrayList.java:462)
	at MemoryError.main(MemoryError.java:12)

6. Conclusion

By now you should be able to know when to use the Endless Loop and how to use it in the most efficient way. You can find the source code on our Github Page.

Related Posts