The compiler can easily and unequivocally prove that the first expression always results in an infinite loop, but it's not as easy for the second. In your toy example it's simple, but what if:

  • the variable's contents were read from a file?
  • the variable wasn't local and could be modified by another thread?
  • the variable relied on some user input?

The compiler is clearly not checking for your simpler case because it's forgoing that road altogether. Why? Because it's much harder forbidden by the spec. See section 14.21:

  • http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21-300-M

(By the way, my compiler does complain when the variable is declared final.)

Answer from Wayne on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ infinite loops in java
Infinite Loops in Java | Baeldung
January 8, 2024 - In this quick tutorial, weโ€™ll explore ways to create an infinite loop in Java. Simply put, an infinite loop is an instruction sequence that loops endlessly when a terminating condition isnโ€™t met.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ infinite loop in java
Infinite Loop in Java - Scaler Topics
July 7, 2024 - In Java, an infinite loop endlessly executes without a stopping condition, potentially causing resource overuse or application crashes. While it can result from programming errors, it might serve intentional purposes based on specific application ...
People also ask

What problems can arise from infinite loops in a Java program?
A: Infinite loops can cause programs to hang, consume excessive resources, and prevent further execution of code.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ infinite loop in java
Mastering Infinite Loop in Java: A Comprehensive Guide with Examples
How can I terminate or break out of an infinite loop in Java?
A: There are several ways to break out of an infinite loop, such as using the `break` statement, modifying loop variables, or using conditional statements to exit the loop.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ infinite loop in java
Mastering Infinite Loop in Java: A Comprehensive Guide with Examples
How can I handle intentional infinite loops to prevent unwanted consequences?
A: Intentional infinite loops should always include termination conditions or mechanisms to break out of the loop based on specific criteria.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ infinite loop in java
Mastering Infinite Loop in Java: A Comprehensive Guide with Examples
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ infinite loop in java
Mastering Infinite Loop in Java: A Comprehensive Guide with Examples
March 30, 2026 - An infinite loop is a loop that continues indefinitely without termination. It can disrupt the execution of a program, leading to excessive resource consumption and application crashes.
๐ŸŒ
Study.com
study.com โ€บ business courses โ€บ java programming tutorial & training
Infinite While Loops in Java - Lesson | Study.com
March 13, 2018 - An infinite while loop in Java is a set of code that would repeat itself forever, unless the system crashes. At a certain point, the data becomes an overload and the program will overflow.
๐ŸŒ
Tutorialsinhand
tutorialsinhand.com โ€บ tutorials โ€บ java-tutorial โ€บ java-loop-control โ€บ infinite-loop.aspx
What is infinite loop in java? | Declare Java Infinite Loop - tutorialsinhand.com
October 31, 2023 - Infinite Loop in Java - Lets learn how to declare infinite loop or what causes infinite loop using for, while or do while in java. We will also see how to avoid infinite loop in java, example of infinite loop in java with video tutorial.
๐ŸŒ
EXLskills
exlskills.com โ€บ courses โ€บ introduction to java โ€บ introduction to java
Infinite Loops | Introduction To Java - EXLskills
August 1, 2018 - ... As discussed previously, it is essential to make sure each loop you write has a distinct end. For example, if the condition inside the for or while loop is always true, the loop will run forever, creating an infinite loop.
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Infinite While Loops in Java | AlgoCademy
Infinite loops are particularly significant in scenarios where the loop's termination condition is not correctly defined or updated. This can happen in various applications, such as data processing, user input handling, and real-time systems. Before diving into infinite loops, let's review the basic structure of a while loop in Java:
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ avoiding-and-understanding-infinite-loops-in-java-b3a294d11a47
Avoiding and Understanding Infinite Loops in Java
August 26, 2024 - Weโ€™ll walk through the mechanics of loops, common mistakes, and strategies to prevent infinite loops. Loops are a fundamental in programming, allowing you to execute a block of code multiple times without needing to write repetitive statements. In Java, loops are used for tasks ranging from simple iterations over arrays to complex algorithms requiring repeated actions. Understanding loops in Java is important for writing efficient and effective code, especially as you go into more advanced programming topics.
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ loops in java
Loops in Java - Scientech Easy
April 4, 2025 - If a loop repeats forever, it is called infinite loops in Java. It is also known as endless loops because the specified condition always becomes true and the body of loop is executed repeatedly, without end.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Java Infinite Loop by Example in a While Looping Structure - Java Programming - Appficial - YouTube
An infinite loop is a loop that will execute indefinitely because the loop's expression is always true. This is a common programming error when creating loop...
Published ย  October 6, 2017
๐ŸŒ
Javatpoint
javatpoint.com โ€บ post โ€บ infinite-loop
Infinite Loop - Javatpoint
Infinite Loop with if-else, switch case, for loop, while loop, do-while, break, continue, goto, arrays, functions, pointers, collections, LinkedList, etc.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-infinite-for-loop
Java - Infinite For Loop
August 6, 2021 - To make a Java For Loop run indefinitely, the condition in for statement has to be true whenever it is evaluated. To make the condition always true, there are many ways. In this tutorial, we will learn some of the ways to create an infinite for loop.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-code-infinite-loop-in-Java
How to code infinite loop in Java - Quora
Answer (1 of 3): Infinite loop means a loop that never ends. In this tutorial, I will show you how to write an infinite loop in Java using for and while loop. While loop to write an infinite loop : โ€˜whileโ€™ loop first checks a condition and then runs the code inside its block.
๐ŸŒ
Opensource.com
opensource.com โ€บ article โ€บ 23 โ€บ 1 โ€บ java-loops
A 4-minute guide to Java loops | Opensource.com
January 14, 2023 - The loop never has a reason to start and count is never incremented. The reverse of this is when a condition starts as true and can never be false, this results in an infinite loop.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ infinite-loop-puzzles-java
Infinite Loop Puzzles in Java - GeeksforGeeks
May 5, 2025 - The loop starts from i=start which is 2,147,483,645 and we know s is close to the maximum int value and when the loop increases i, it reaches Integer.MAX_VALUE and the next increment causes overflow and wrap around to Integer.MIN_VALUE. The loop will keep on running because the condition i <= s + 1 remains true due to the overflow, which cause an infinite loop. This loop will never end because of the behavior of integer overflow. Java can aslo work with primitive types and objects.