data. Primitive data types are Java’s eight built-in data types. Figure 4-4 shows the size for each primitive data type as well as the minimum and maximum values each can hold. Java Primitive Data Types Four of the primitive data types can hold integers: byte, short, int, and long. These four data types differ in their memory size. Because values are stored in binary, the number of bytes allocated to a data type determines its maximum and minimum values. In a program that manages large amounts of data, execu- tion time and storage space can be saved by allocating the data type with the fewest bytes required to hold the largest or smallest value. If the value is an in- teger, choose and either use the byte, short, int, or long data type. To simplify, in this text, the int data type will be used when the data is an integer. The float and double data types hold real numbers. A real number is a num- ber with a fractional part. That fractional part can be expressed with a decimal point, as in 415.67, or with scientific E notation, 4.1567E2. Notice that 4.1567E2 = 4.1567 × 102. If the value is a real number, choose and use either the float and double data type. The float and double data types differ in the number of digits of precision that can be stored in that data type. The float data type holds what is called single FYI Real numbers are also called floating-point numbers because of the decimal points. The name of the data type float comes from this concept. Goodheart-Willcox Publisher Figure 4-3. These examples show valid and invalid identifiers. Valid Valid Not Not Valid Valid Identifier Explanation Identifier Explanation george all letters Janette Jones spaces are not allowed Alisha_Jones underscores are allowed 2small numbers cannot start an identifier big$Cost dollar signs are allowed public public is a Java keyword my3rdAttempt numbers are allowed after the first character im@school the @ character is not allowed Goodheart-Willcox Publisher Figure 4-4. Java contains eight built-in data types called primitive data types. Data Type Data Type Number Number of Bytes of Bytes Minimum Value Minimum Value Maximum Value Maximum Value Integers byte 1 –128 +127 short 2 –32,768 +32,767 int 4 –2,147,483,648 +2,147,483,647 long 8 approximately –9 quintillion approximately +9 quintillion Real Numbers float 4 +1.4E-45 (minimum positive nonzero value) +3.4028235E38 double 8 +4.9E-324 (minimum positive nonzero value) +1.7976931348623157E308 Characters char 2 0000 (hex) FFFF (hex) Boolean boolean undefined N/A N/A Copyright Goodheart-Willcox Co., Inc. 76 Introduction to Computer Science: Java Programming
Previous Page Next Page