Win a copy of Learn Spring Security (video course) this week in the Spring forum!

# limiting value?

Ali Gilani
Ranch Hand
Posts: 137

when u run the code, from the 16 loop onwards, number takes a constant value. why is that? using long a limiting value is also reached. why is that? is it only a DOS thing? if u make a GUI window and display number in that will it go on displaying the numbers?

Ali

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
The largest value an int can represent is 2^31-1; the largest value a long can represent is 2^63-1. They are stored in a finite number of machine bits, so there's a finite magnitude they can represent.

The float and double data types have much larger ranges, but lower precision.

Ali Gilani
Ranch Hand
Posts: 137
so if i want to view beyond the limits??

and what do u mean by lower precision? if the value is 2343443545 will double show it as 2343443545.0 or 2.3e9?

Ali

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
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 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.