Page 470 - Introduction to Programming with Java: A Problem Solving Approach
P. 470

                436
Chapter 11 Type Details and Alternate Coding Mechanisms
 /*****************************************************************
*
ByteOverflowDemo.java
Dean & Dean
This demonstrates integer overflow.
*****************************************************************/
*
*
*
public class ByteOverflowDemo
{
}
// end ByteOverflowDemo class
public static void main(String[] args)
{
}
byte value = 64;
System.out.println("Initial byte value = " + value);
System.out.println("Byte maximum = " + Byte.MAX_VALUE);
value += value;
System.out.println("Twice initial byte value = " + value);
// end main
Output:
Initial byte value = 64
 Byte maximum = 127
Apago PDF Enhancer
Twice initial byte value = -128
Averylargeerror!
Figure 11.2
ByteOverflowDemo program illustrates the overflow problem
 Type
 Storage
 Precision
 Wrapper Class’s
MIN_NORMAL
 Wrapper Class’s
MAX_VALUE
 float
 32 bits
 6 digits
 􏰀 1.2 * 10􏰅38
 􏰀 3.4*1038
 double
 64 bits
 15 digits
 􏰀 2.2 * 10􏰅308
 􏰀 1.8*10308
  Figure 11.3
Properties of Java floating-point data types
Note Figure 11.3’s precision column. Precision refers to the approximate number of digits the type can represent accurately. For example, since float types have 6 digits of precision, if you attempt to store 1.2345678 in a float variable, you would actually store a rounded version—a number like 1.234568. The first six digits (1.23456) are precise, but the rest of the number is imprecise. double values have 15 digits of precision—quite a bit better than float values with their 6 digits of precision. The relatively low precision of a float can lead to significant round-off errors when you subtract two numbers that are close in value. If the numbers are close enough, then the difference is a very small number where the rightmost















































   468   469   470   471   472