Ali Gilani

Ranch Hand

Posts: 137

Ali Gilani

Ranch Hand

Posts: 137

posted 11 years ago

Before we get into this: note that none of this is unique to Java. Many computer languages -- especially those related to FORTRAN and C -- behave this way.

Integers "wrap around". If you add one to the largest possible integer, you don't get what you expect. This is called "overflow". If you add one to the largest positive int in Java, you actually get the largest possible negative int -- i.e., (2^31-1) + 1 -> -2^31 . This is a result of something called "two's complement" notation -- the way that negative numbers are represented.

ints and longs are exact, while floating-point numbers (float and double) are not. Remember that between any two real numbers, there are an infinity of intermediate values. As a result, no finite computer representation can represent an arbitrary real number exactly. The "exactness" of a floating-point representation is called its

Here's an overview article at Wikipedia which explains all of these concepts.

Integers "wrap around". If you add one to the largest possible integer, you don't get what you expect. This is called "overflow". If you add one to the largest positive int in Java, you actually get the largest possible negative int -- i.e., (2^31-1) + 1 -> -2^31 . This is a result of something called "two's complement" notation -- the way that negative numbers are represented.

ints and longs are exact, while floating-point numbers (float and double) are not. Remember that between any two real numbers, there are an infinity of intermediate values. As a result, no finite computer representation can represent an arbitrary real number exactly. The "exactness" of a floating-point representation is called its

*precision*. floats have 20-something bits of precision, and doubles have 50-something. Because a long is 64 bits, that means that there isn't a unique double value for each possible long value -- i.e., some adjacent long values correspond to the same double value.Here's an overview article at Wikipedia which explains all of these concepts.