go to previous page   go to home page   go to next page hear noise highlighting

Answer:


Numeric Primitive Data Types

Numbers are so important in Java that six of the eight primitive data types are numeric types.

There are both integer and floating point primitive types. Integer types have no fractional part; floating point types have a fractional part. On paper, integers have no decimal point, and floating point types do. But in main memory, there are no decimal points. Even floating point values are represented with bit patterns. There is a fundamental difference between the method used to represent integers and the method used to represent floating point numbers.

Each primitive type uses a fixed number of bits. This means that if you are using a particular data type then the same number of bits will be used no matter what value is represented.

For example, all values represented using the short data type use 16 bits. The value zero (as a short) uses 16 bits and the value thirty thousand uses 16 bits.

All values represented using the long data type use 64 bits. The value zero (as a long) uses 64 bits, the value thirty thousand uses 64 bits, and the value eight trillion uses 64 bits.

Values that are large in magnitude (negative or positive) need more bits to be represented. This is similar to writing out numbers on paper: large numbers need more digits. If a value needs more bits than a particular data type uses, then it cannot be represented using that data type.

In the tables, E means "ten to the power of". So 3.5E38 means 3.5 x 1038



Integer Primitive Data Types
TypeSizeRange
byte8 bits-128 to +127
short16 bits-32,768 to +32,767
int32 bits-2 billion to +2 billion (approximately)
long64 bits-9E18 to +9E18 (approximately)




Floating Point Primitive Data Types
TypeSizeRange
float32 bits-3.4E38 to +3.4E38
double64 bits-1.7E308 to 1.7E308


QUESTION 6:

Say that you want to deal with the number 1,023,004 in your computer program. Would data type short be an appropriate choice?


go to previous page   go to home page   go to next page