Incremental Java
Primitive Types: Double

Double Types

One problem with int is that it can only represent integers. We need something like real numbers.

Computers can't represent mathematical real numbers. Like integers, real numbers can be infinitely large. Computers, having finite memory, can't store infinite sized real numbers. They can't even store extremely large real numbers.

There's a second problem. Real numbers can have an infinite number of digits after the decimal point. Again, computers have finite memory, so you can't store real numbers to infinite precision.

Computers use floating point notation to store real numbers. If you've taken a course in chemistry or physics, you should have studied exponential or scientific notation.

You write numbers like 2.3 x 103. 2.3 is called the mantissa. In scientific notation this number must be greater than or equal to 0, but less than 10. 3 is the exponent. It can be any value.

In Java, the type of floating point is called double. This is short for "double precision floating point". Doubles have a huge range, from about 10-308 to 10308. That's 10 followed by 300 zeroes. It's more than you really need.

Why Int?

Now you might ask yourself why bother with int. Doesn't double handle both?

Several reasons we use int. First, int uses less space. One int uses up 32 bits, while one double uses 64 bits.

Second, the hardware for doing multiplication and addition is quicker for int than double. Although you're not likely to notice the speed difference, a program using lots of floating point multiplications would be slower.

Third, int is more precise. As long as you stay in between the minimum and maximum value, you have precise arithmetic using int. double, on the other hand, has missing numbers. In particular, recall we can only have a finite number of digits after the decimal point.

Just to illustrate the concept. Suppose we could only use 3 digits past the decimal point. Then, both 3.1442 and 3.1443 (both of which have 4 digits past the decimal point) would be stored in a computer as 3.144. We had to get rid of the fourth digit because there was no space. This introduces a tiny error.

If you do computations in a bad way, this error begins to magnify.

This doesn't happen with int, provided you don't need to use fractions.

The rule of thumb is: use int if possible, and double only if necessary.

How to Write Double Literals

Java needs to know when you're writing an int literal and when you're writing a double literal. To make the distinction, you must put a decimal point for double literals.

Thus 4 is an int literal, but 4.0 is a double literal. In general, I put at least one digit before the decimal point and one afterwards. It makes it easier to read. However, it isn't required. Thus, 4. is valid and so is .3. In my opinion, you should write it as 4.0 and 0.3.

There are other ways to write double literals, but for now, let's not look at the other ways.

And, yes, double literals can be negative, by putting a negative sign in front, such as 3.1415.

Just as with int literals, do not use commas for large values. You'd write 1923412.23 with no commas. Adding commas is an error in Java.

Double Literals Are Not Unique

You can write 3.14 or 3.140 or 3.140000. All of this is the same to Java. Extra zeroes that don't change the value are not stored.

Basically, Java converts these values to some internal format, where the additional zeroes don't play a factor. Of course, if you have 0.34 and 0.034, these are clearly different numbers so they are stored differently.

The point of this discussion is to let you know that Java allows you to write double values in many different equivalent ways (mostly by adding zeroes at the very beginning or end).

This is not a very important point we're making, but it's useful to know.