22204

Why incrementing int primitive in while loop does not loop forever

I have a following code sample

int i = 1; while(i != 0) { i++; }

I was expecting this to run in an infinite loop but it didn't. Then when I printed the value after while loop I got:

i value is 0.

Can any one let me know what exactly is happening?

Answer1:

I was expecting this to run in an infinite loop but it didn't.

No, it wouldn't. Eventually the value will become 0 again.

In particular, it will logically execute like this:

1 2 3 ... 2,147,483,646 2,147,483,647 -2,147,483,648 // Integer overflow => wrapping -2,147,483,647 ... -3 -2 -1 0

... then the loop will end

From section 15.18.2 of the JLS:

If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format. If overflow occurs, then the sign of the result is not the same as the sign of the mathematical sum of the two operand values.

There are no memory issues with this code because it doesn't perform any allocations - it just increments a local variable, entirely on the stack.

Now whether or not it actually does that is a different matter - if the JIT is able to work out what the behaviour will be without executing every iteration, it can optimize it away - but that's an implementation detail. The important part is understanding the logic behind the behaviour.

Answer2:

An int is an int: For each i, if i is an int so is i+1. An int is always 32-bits, it never requires a larger container.

Last, your loop is not infinite.

Recommend

  • Integral constant is too large (CS1021) - How to put 1000 extremely big (70+ digits) integers in an
  • tostring() is implicitly called… how?
  • SQL - Query to seperate single column and create new columns based on column data
  • Passing char into a method with an int parameter
  • T-SQL - Minutes per hour between two datetimes
  • Why no Autoboxing while removing primitive type from a List in Java?
  • display unique field out of 10 records in sql
  • hex to string formatting conversion in python
  • Mach-O Linker Error When Adding Unit Tests - XCode
  • Width of int64_t, is it always 64 bits?
  • How to return bits from Byte
  • Determining signed state for HDF5 variables in NetCDF
  • Loading a TGA File and Using it with OpenGL
  • How do I create a M2Crypto DSA object given parameters and key values?
  • Java Generic Type Inference Strange Behavior?
  • Learn Ruby Hard Way ex. 48
  • Python : How to center Label in tkinter window
  • byte, char, int in Java - bit representation
  • Greek letters in a GUI - PYTHON
  • Aspect not executed in Spring
  • Why is RAM in powers of 2?
  • How to extract a plane from a 3D variable in FiPy (3D to 2D)
  • How to make nicEditor snaplet? (Several questions)
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • c++ using primitive types as a base class
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Creating NSCollectionView with datasource programatically
  • Get the number 18437736874454810627
  • Aptana 3 remove bundle (jquery)
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • Uncaught TypeError: $(…).select2 is not a function
  • DIV instruction jumping to random location?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Z3: Convert between FP and BitVector?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Groovy: Unexpected token “:”
  • Counter field in MS Access, how to generate?
  • SSO with signing and signature validation doesn't work
  • Convert array of 8 bytes to signed long in C++
  • Statically linking a C++ library to a C# process using CLI or any other way