Infinite Loop Puzzles in Java

Problem 1 : Insert code in the given code segments to make the loop infinite.

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG
{
      public static void main(String s[]){
  
           /* Insert code here */
  
           for (int i = start; i <= start + 1; i++) {
  
             /* Infinite loop */
           }
      }
}

chevron_right


Solution:
It looks as though it should run for only two iterations, but it can be made to loop indefinitely by taking advantage of the overflow behavior.
Integer.MAX_VALUE is the maximum value that an int can store in Java. When i gets to Integer.MAX_VALUE and is incremented, it silently wraps around to Integer.MIN_VALUE. So, we can declare variable start with 1 less than maximum value.
Following is the solution:

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG
{
      public static void main(String s[]){
  
           int start = Integer.MAX_VALUE-1;
           for (int i = start; i <= start + 1; i++) {
             /* Infinite loop */
           }
      }
}

chevron_right


In this, start=2147483645 (Integer.MAX_VALUE-1), and the value goes like 2147483645, 2147483646, -2147483648, -2147483647…….. and so on.

 

Problem 2 Insert code in the given code segments to make the loop infinite.

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG
{
      public static void main(String s[]) {
  
           /* Insert code here */
  
           while (i <= j && j <= i && i != j) {
  
            /* Infinite loop */
  
           }
      }
}

chevron_right


Solution:
Until release 5.0, Java’s numerical comparison operators (=) required both of their operands to be of a primitive numeric type (byte, char, short, int, long, float, or double). In release 5.0, the specification was changed to say that the type of each operand must be convertible to a primitive numeric type. Therein lies the rub.
In release 5.0, autoboxing and auto-unboxing were added to the language. We are using this in the following :

filter_none

edit
close

play_arrow

link
brightness_4
code

class GFG
{
      public static void main(String s[]){
          Integer i = new Integer(0);
          Integer j = new Integer(0);
           while (i <= j && j <= i && i != j) {
            /* Infinite loop */
           }
      }
}

chevron_right


The first two subexpressions (i <= j and j <= i) perform unboxing conversions on i and j and compare the resulting int values numerically. Both i and j represent 0, so both of these subexpressions evaluate to true. The third subexpression (i != j) performs an identity comparison on the object references i and j. The two variables refer to distinct objects, as each was initialized to a new Integer instance. Therefore, the third subexpression also evaluates to true, and the loop spins forever.

This article is contributed by Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.