Section 3.3. Data Types in Java

   

3.3 Data Types in Java

Java uses different types to store data; it is thus referred to as a strongly typed language. The range of values for these types is based on the number 2 raised to a power equal to the number of bits for each type. An 8-bit number therefore has 2 8 possible values, or 256 values. You may have used a bit data type in an SQL database to represent a value such as isOnSpecial where 0 means the item is not currently on special, and 1 means it is. Since 2 1 = 2, we see that a 1-bit number (bit) has a range of possible values of 2 ”it can be 0 or 1.

However, when determining the range of possible values for a data type in Java, one of the bits in the result is saved to represent the number's sign (whether it is positive or negative). Therefore, an 8-bit number does not represent the 256 values 0 to 255. It represents the values “128 to +127. The highest value possible for an 8-bit number is going to be 2 to the power of the number of bits minus 1, minus 1. For an 8-bit number, this is represented 2 8 “1 “1, or 2 7 “1. So, 2 7 is 128, minus 1 is 127. It is necessary to subtract the 1 because zero is counted as positive. This is an important aspect of working with Java that can be hard to remember at first: things generally start at 0, not 1.

You can use this formula to calculate the range of possible values for a given data type: ( “2 x “1 to 2 x “1 “1).

As mentioned above, Java has predefined data types that allow for only certain values to be stored in them (such as numeric or character values), and those data types have a predefined range expressed in a number of bits, of values they are capable of holding. Different kinds of information are stored in different data types in Java.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net