Reserving Memory


Although a unit of memory holds a byte, data used in a program can be larger than a byte and require 2, 4, or 8 bytes to be stored in memory. Before any data can be stored in memory, you must tell the computer how much space to reserve for data by using an abstract data type.

An abstract data type is a keyword of a programming language that specifies the amount of memory needed to store data and the kind of data that will be stored in that memory location. However, an abstract data type does not tell the computer how many bytes to reserve for the data. The number of bytes reserved for an abstract data type varies, depending on the programming language used to write the program and the type of computer used to compile the program.

Abstract data types in Java have a fixed size in order for programs to run in all Java runtime environments. In C and C++, the size of an abstract data type is based on the register size of the computer used to compile the program. The int and float data types are the size of the register. A short data type is half the size of an int , and a long data type is double the size of an int .

Think of an abstract data type as the term case of tomatoes. You call the warehouse manager and say that you need to reserve enough shelf space to hold five cases of tomatoes. The warehouse manager knows how many shelves to reserve because she knows the size of a case of tomatoes.

The same is true about an abstract data type. You tell the computer to reserve space for an integer by using the abstract data type int . The computer already knows how much memory to reserve to store an integer.

The abstract data type also tells the computer the kind of data that will be stored at the memory location. This is important because computers manipulate data of some abstract data types differently than data of other abstract data types. This is similar to how the warehouse manager treats a case of paper plates differently than a case of tomatoes.

Table 1-2 contains a list of abstract data types. The first column contains keywords for each abstract data type. The second column lists the corresponding number of bits that are reserved in memory for a Java program. The third column shows the range of values that can be stored in the abstract data type. And the last column is the group within which the abstract data type belongs.

Table 1-2: Simple Java Data Types.

Data Type

Data Type Size in Bits

Range of Values

Group

 byte 

8

“128 to 127

Integers

 short 16 

16

“32,768 to 32,767

Integers

 int 32 

32

“2,147,483,648 to 2,147,483,647

Integers

 long 64 

64

“9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Integers

 char 16 (Unicode) 

16 (Unicode)

65,536 (Unicode)

Characters

 float 32 

32

3.4e-038 to 3.4e+038

Floating-point

 double 64 

64

1.7e-308 to 1.7e+308

Floating-point

 boolean 1 

1

0 or 1

Boolean

You choose the abstract data type that best suits the data that you want stored in memory, then use the abstract data type in a declaration statement to declare a variable. A variable is a reference to the memory location that you reserved using the declaration statement (see Chapter 2).

You should always reserve the proper amount of memory needed to store data because you might lose data if you reserve too small a space. This is like sending ten cases of tomatoes to the warehouse when you only reserved space for five cases. If you do this, the other five cases will get tossed aside.

Abstract Data Type Groups

You determine the amount of memory to reserve by determining the appropriate abstract data type group to use and then deciding which abstract data type within the group is right for the data.

There are four data type groups:

  • Integer Stores whole numbers and signed numbers . Great for storing the number of dollars in your wallet when you don t need a decimal value.

  • Floating-point Stores real numbers ( fractional values). Perfect for storing bank deposits where pennies (fractions of a dollar) can quickly add up to a few dollars.

  • Character Stores a character. Ideal for storing names of things.

  • Boolean Stores a true or false value. The correct choice for storing a yes or no or true or false response to a question.

Integers

The integer abstract data type group consists of four abstract data types used to reserve memory to store whole numbers : byte , short , int , and long , as described in Table 1-2.

Depending on the nature of the data, sometimes an integer must be stored using a positive or negative sign, such as a +10 or “5. Other times an integer is assumed to be positive so there isn t any need to use a positive sign. An integer that is stored with a sign is called a signed number; an integer that isn t stored with a sign is called an unsigned number .

What s all this hoopla about signed numbers? The sign takes up 1 bit of memory that could otherwise be used to represent a value. For example, a byte has 8 bits, all of which can be used to store an unsigned number from 0 to 255. You can store a signed number in the range of “128 to +127.

C and C++ support unsigned integers. Java does not. An unsigned integer is a value that is implied to be positive. The positive sign is not stored in memory. All integers in Java are represented with a sign. Zero is stored as a positive number.

byte Abstract Data Type

The byte abstract data type is the smallest abstract data type in the integer group and is declared by using the keyword byte (see Figure 1-2). Programmers typically use a byte abstract data type when sending data to and receiving data from a file or across a network. The byte abstract data type is also commonly used when working with binary data that may not be compatible with other abstract data types. Choose a byte whenever you need tomove data to and from a file or across a network.

click to expand
Figure 1-2: A byte abstract data type in Java reserves 8 bits of main memory.
short Abstract Data Type

The short abstract data type is ideal for use in programs that run on 16-bit computers. However, most of those computers are on the trash heap and have been replaced by 32-bit and 64-bit computers! (See Figure 1-3.) Therefore, the short is the least used integer abstract data type. Choose a short if you ever need to store an integer in a program that runs on a very old computer.

click to expand
Figure 1-3: A short abstract data type in Java reserves 16 bits of main memory.
int Abstract Data Type

The int abstract data type is the most frequently used abstract data type of the integer group for a number of reasons (see Figure 1-4). Choose an int :

  • For control variables in control loops

  • In array indexes

  • When performing integer math

    click to expand
    Figure 1-4: An int abstract data type in Java reserves 32 bits of main memory.

long Abstract Data Type

A long abstract data type (see Figure 1-5) is used whenever using whole numbers that are beyond the range of an int data type (refer to Table 1-2). Choose a long when storing the net worths of Bill Gates, Warren Buffet, and you in a program.

Floating-Point

Abstract data types in the floating-point group are used to store real numbers in memory. A real number contains a decimal value. There are two kinds of floating point data types: float and double (as described in Table 1-2). The float abstract data type is a single precision number, and a double is a double precision number. Precision of a number is the number of places after the decimal point that contains an accurate value.

click to expand
Figure 1-5: A long abstract data type in Java reserves 64 bits of main memory.

The term floating-point refers to the way decimals are referenced in memory. There are two parts of a floating-point number: the real number, which is stored as a whole number, and the position of the decimal point within the whole number. This is why it is said that the decimal point floats within the number.

For example, the floating-point value 43.23 is stored as 4323 (no decimal point). Reference ismade in the number indicating that the decimal point is placed after the second digit.

float Abstract Data Type

The float abstract data type (see Figure 1-6) is used for real numbers that require single precision, such as United States currency. Single precision means the value is precise up to 7 digits to the right of the decimal. For example, suppose you divide $53.50 evenly among 17 people. Each person would get $3.147058823529. Digits to the right of $3.1470588 are not guaranteed to be precise because of the way a float is stored in memory. Choose a float whenever you need to store a decimal value where only 7 digits to the right of the decimal must be accurate. double Abstract Data Type The double abstract data type (see Figure 1-7) is used to store real numbers that are very large or very small and require double the amount of memory that is reserved with a float abstract data type. Choose a double whenever you need to store a decimal value where more than 7 digits to the right of the decimal must be accurate.

click to expand
Figure 1-6: A float abstract data type in Java reserves 32 bits of main memory.
click to expand
Figure 1-7: A double abstract data type in Java reserves 64 bits of main memory.

Characters

A character abstract data type (see Figure 1-8) is represented as an integer value that corresponds to a character set. A character set assigns an integer value to each character, punctuation, and symbol used in a language.

click to expand
Figure 1-8: A char abstract data type in Java reserves 16 bits of main memory.

For example, the letter A is stored in memory as the value 65, which corresponds to the letter A in a character set. The computer knows to treat the value 65 as the letter A rather than the number 65 because memory was reserved using the char abstract data type. The keyword char tells the computer that the integer stored in that memory location is treated as a character and not a number.

There are two character sets used in programming, the American Standard Code for Information Interchange (ASCII) and Unicode. ASCII is the granddaddy of character sets and uses a byte to represent a maximum of 256 characters. However, a serious problem was evident after years of using ASCII. Many languages such as Russian, Arabic, Japanese, and Chinese have more than 256 characters in their language. A new character set called Unicode was developed to resolve this problem. Unicode uses 2 bytes to represent each character. Choose a char whenever you need to store a single character in memory.

Boolean Abstract Data Type

A boolean abstract data type (see Figure 1-9) reserves memory to store a boolean value, which is a true or false represented as a zero or one. Choose a boolean whenever you need to store one of two possibilities in memory.

click to expand
Figure 1-9: A boolean abstract data type in Java reserves 1 bit of main memory.



Data Structures Demystified
Data Structures Demystified (Demystified)
ISBN: 0072253592
EAN: 2147483647
Year: 2006
Pages: 90

Similar book on Amazon

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