March 26, 2008

Plano: Dallas overflowing

Filed under: Uncategorized — dave @ 2:24 pm

I just had a discussion with a colleague regarding java’s handling of data types and it reminded me of a strange problem I had building my phone records application.

When I started making the database schema I thought to myself “Phone numbers are really just integers, so I should store them as such.” So I ran the perl script I was using at the time to convert t-mobile’s CSV file of my call records into SQL statements. I ran the script and MySQL’s curious decision to just truncate data raised its ugly head.

The first query I ran against the database revealed I was making an awful lot of calls to some Dallas number — (214) 748-3647. I punched it into my phone to figure out who on earth that was, since I would have bet money that I called my parent’s home phone number more than any other number. It wasn’t in my phone.

The problem of course is that integers are normally 32 bits and half the numbers representable by an int are negative, giving a range of -2^31 -> 2^31-1, or -2147483648 -> 2147483647. So the next time you’re trying to remember how big an int is just remember that Integer.MAX is a valid Dallas phone number.