Infinite loops in Java

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.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)