Page 103 - Introduction to Programming with Java: A Problem Solving Approach
P. 103
3.13 Numeric Data Types—int, long, float, double 69 Here’s an alternative way to do the same thing using declaration and assignment statements (instead of
using initialization statements):
String name;
int creditHours;
name = "John Doe";
creditHours = 0;
// student's name
// student's total credit hours
It’s OK to use either technique—initialization or declaration/assignment. You’ll see it done both ways in the real world. Initialization has the benefit of compactness. Declaration/assignment has the benefit of leaving more room in the declaration for a comment.
3.13 Numeric Data Types—int, long, float, double Integers
We’ve already mentioned one Java numeric data type—int. We’ll now discuss numeric types in more detail. Variables that hold whole numbers (e.g., 1000, 22) should normally be declared with the int data type or the long data type. A whole number is a number with no decimal point and no fractional component.
An int uses 32 bits of memory. A long uses 64 bits of memory (twice as many bits as an int). The
range of values that can be stored in an int variable is approximately 2 billion to 2 billion. The range of
values that can be stored in a long variable is approximately 9 1018 to 9 10 18. Here’s an example that
declares studentId to be an int variable and satelliteDistanceTraveled to be a long variable: Apago PDF Enhancer
int studentId;
long satelliteDistanceTraveled;
If you attempt to store a really big number (a number over 2 billion) in an int variable, you’ll get an
“integer number too large” error when you compile your program. So to be safe, why shouldn’t you just
always declare your integer variables as type long rather than type int? An int takes up less storage in
memory. And using less storage means your computer will run faster because there’s more free space. So in
the interest of speed/efficiency, use an int rather than a long for a variable that holds values less than 2 10
billion. If you’re not sure whether a variable will hold values greater than 2 billion, play it safe and use a long. If you want the greatest possible precision in financial calculations, convert everything to cents, and use long variables to hold all values.
Floating-Point Numbers
In Java, numbers that contain a decimal point (e.g., 66. and 1234.5) are called floating-point numbers. Why? Because a floating-point number can be written with different forms by shifting (floating) its decimal point. For example, the number 1234.5 can be written equivalently as 1.2345 103. See how the deci- mal point has “floated” to the left in the second version of the number?
There are two types for floating-point numbers—float and double. A float uses 32 bits of mem- ory. A double uses 64 bits of memory. A double is called a “double” because it uses twice as many bits as a float.
10 The suggestion to use an int for efficiency reasons is valid, but be aware that the speed difference is only occasionally noticeable. It’s only noticeable if you’ve got lots of long numbers and you’ve got a small amount of available memory, such as when you’re run- ning a program on a personal digital assistant (PDA).