Page 104 - Introduction to Programming with Java: A Problem Solving Approach
P. 104
70 Chapter 3 Java Basics
Here’s an example that declares gpa as a float variable and cost as a double variable:
float gpa;
double cost;
The double data type is used much more often than the float data type. You should normally de- clare your floating-point variables to be double rather than float because (1) double variables can hold a wider range of numbers11 and (2) double variables can store numbers with greater precision. Greater precision means more significant digits. You can rely on 15 significant digits for a double variable but only 6 significant digits for a float variable.
Six significant digits may seem like a lot, but for many cases, six significant digits are not enough. With only six significant digits, accuracy errors can creep into float-based programs whenever there’s a math- ematical operation (addition, multiplication, etc.). If such a program performs a significant number of math- ematical operations, then the accuracy errors become nontrivial. So as a general rule, use double rather than float for programs that perform a significant number of floating-point mathematical operations. And since accuracy is particularly important with money, scientific measurements, and engineering measure- ments, use double rather than float for calculations that involve those items.
Assignments Between Different Types
You’ve learned about assigning integer values into integer variables and floating-point values into floating- point variables, but you haven’t learned about assignments where the types are different.
Assigning an integer value into a floating-point variable works just fine. Note this example:
double bankAccountBalance = 1000;
Assigning an integer value into a floating-point variable is like putting a small item into a large box. The int type goes up to approximately 2 billion. It’s easy to fit 2 billion into a double “box” because a double goes all the way up to 1.8 10 308.
On the other hand, assigning a floating-point value into an integer variable is like putting a large item
12
into a small box. By default, that’s illegal.
int temperature = 26.7;
For example, this generates an error:
Apago PDF Enhancer
Since 26.7 is a floating-point value, it cannot be assigned into the int variable, temperature. That should make sense when you realize that it’s impossible to store .7, the fractional portion of 26.7, in an int. After all, int variables don’t store fractions; they store only whole numbers.
This statement also generates an error:
int count = 0.0;
The rule says that it’s illegal to assign a floating-point value into an integer variable. 0.0 is a floating-point value. It doesn’t matter that the fractional portion of 0.0 is insignificant (it’s .0); 0.0 is still a floating-point value, and it’s always illegal to assign a floating-point value into an integer variable. That type of error is known as a compile-time error or compilation error because the error is identified by the compiler during the compilation process.
Later in the book, we provide additional details about integer and floating-point data types. You don’t need those details now, but if you can’t wait, you can find the details in Chapter 11, Section 11.2.
11 A float variable can store positive values between 1.2 1038 and 3.4 1038 and negative values between 3.4 1038 and
308
1.2 10
1.8 10
12 Although such an assignment is normally illegal, you can do it if you add some code. Specifically, you can do it if you add a cast operator. We’ll describe cast operators later in this chapter.
38 308
308
and negative values between
. A double variable can store positive values between 2.2 10
and 1.8 10
and2.2 10
308
.