3. Numeric Data and Operators

Java has two kinds of numeric data: integers, which have no fractional part, and real numbers or floating-point numbers, which contain a fractional component. Java recognizes four different kinds of integers: byte, short, int, and long, which are distinguished by the number of bits used to represent them. A binary digit, or bit, is a 0 or a 1. (Recall that computers read instructions as series of 0s and 1s.) Java has two different kinds of real numbers, float and double, which are also distinguished by the number of bits used to represent them. See Table 5.3.

TABLE 5.3 Java's numeric types
Type Bits Range of Values
byte 8 -128 to +127
short 16 -32768 to 32767
int 32 -2147483648 to 2147483647
long 64 -263 to 263 -1
float 32 -3.40292347E+38 to + 3.40292347E +38
double 64 -1.79769313486231570E +308 to + 1.79769313486231570E+308

public class OneRowNim
{     private int nSticks = 7;
      private boolean onePlaysNext = true;
      public OneRowNim()                   {
      } //OneRowNim() constructor1
      public OneRowNim(int sticks)
     { nSticks = sticks;     } // OneRowNim() constructor2
      public OneRowNim(int sticks, int starter)
      { nSticks = sticks;
       onePlaysNext = (starter - 1); } // OneRowNim() constructor3
     public boolean takeSticks(int num)
    { if (num < 1 num > 3 II num > nSticks)
            return false;     // Error
       else                    // valid move
      {  nSticks = nSticks num;
         onePlaysNext = ! onePlaysNext;
         return true;
        } // else } // takeSticks ()     
    public int getSticks ()
    { return nSticks; } {\ color{cyan} // getSticks
    public int getPlayer ()
    { if (onePlaysNext) return 1;
      else return 2;                
   } // getPlayer ()        
   public boolean gameOver()
  { return (nSticks <= 0);
  } // gameOver ()
  public int getWinner ()
  { if (nSticks < 1) return getPlayer ();
    else return 0; // game is not over
  } // getWinner ()
  public void report ()
  { System. out. println ("Number of sticks left :                   +  getSticks ());
System. out . println ("Next turn by player"
                         + getPlayer ());  
   } // report ()               
  } // OneRowNim class

Figure 5.2: The revised OneRowNim uses a boolean variable to keep track of who's turn it is.

The more bits a data type has, the more values it can represent. One bit can represent two possible values, 1 and 0, which can be used to stand for true and false, respectively. Two bits can represent four possible values: 00, 01, 10, and 11; three bits can represent eight possible values: 000, 001, 010, 100, 101, 110, 011, 111. And, in general, an n-bit quantity can represent 2n different values.


Integer data types

As illustrated in Table 5.3, the various integer types represent positive or negative whole numbers. Perhaps the most commonly used integer type in Java is the int type, which is represented in 32 bits. This means that Java can represent 232 different int values, which range from 2,147,483,648 to 2,147,483,647, that is, from - 231 to (231 -1). Similarly, an 8-bit integer, a byte, can represent 28 or 256 different values, ranging Integer data types from 128 to +127. A 16-bit integer, a short, can represent 216 different values, which range from 32768 to 32767. And a 64-bit integer, a long, can represent whole number values ranging from 263 to 263 -1.

For floating-point numbers, a 32-bit float type can represent 232 different real numbers and a 64-bit double value can represent 264 different real numbers.

JAVA EFFECTIVE DESIGN Platform Independence. In Java, a data type's size (number of bits) is part of its definition and, therefore, remains consistent across all platforms. In C and C++, the size of a data type is dependent on the compiler.


Data types are abstractions

It is worth noting that just as model airplanes are representations of real airplanes, Java's numeric types are representations or models of the numbers we deal with in mathematics. In designing Java's data types, various trade-offs have been made in order to come up with practical implementations.


Representation trade-offs

One trade-off is that the set of integers is infinite, but Java's int type can only represent a finite number of values. Similarly, Java cannot represent the infinite number of values that occur between, say, 1.111 and 1.112. So, certain real numbers cannot be represented at all. For example, because Java uses binary numbers to represent its numeric types, one number that cannot be represented exactly is 1/10 . This inability to exactly represent a value is known as round-off error. Being unable to represent certain values can cause problems in a program. For example, it might be difficult to represent dollars and cents accurately in a program.


Round-off error

Another source of problems in dealing with numeric data is due to limits in their precision. For example, a decimal number represented as a double value can have a maximum of 17 significant digits, and a float can have a maximum 8. A significant digit is one that contributes to the number's value. If you tried to store values such as 12345.6789 or 0.123456789 in a float variable, they would be rounded off to 12345.679 and 0.12345679, respectively, causing a possible error.

JAVA DEBUGGING TIP Significant Digits. In using numeric data, be sure the data type you choose has enough precision to represent the values your program needs.