Flylib.com

Books Software

 
 
 

Chapter 6. PRIMITIVE TYPES

     

Chapter 6. PRIMITIVE TYPES

DO OR DIE:

  • Dig the primitives.

  • Yeah, baby, I'm lovin' the primitives.


This short topic introduces the eight primitive types in Java.

     

About Java Primitives

Java primitives are not objects. They are simple values. They are used to represent single characters , numbers , and the logical values true and false.

Primitive types in Java have a non-trivial benefit over primitives in languages such as C and C++: the ranges of Java primitive types are not dependent on the underlying system. They do not change from system to system as you port your app, because of the Java Virtual Machine. This keeps you from the dangers of overflow problems you can run into in those languages. When you specify an int in C or C++, you don't know what the actual range is; your program will have to use whatever the range of the primitive is on the target platform. For example, in Java, you don't have to worry that on a 16-bit processor, your int will allow only a 16-bit range, but on a more recent processor, 64 bits. This could cause serious problems for your data, which would not be truncated, but would instead wrap .

Java stores its data as different types for different kinds of things. That's why it's called a strongly typed language.

For the numeric number, the lower range is calculated as 2 to the power of the number of bits minus 1. The upper range of the data types is calculated as 2 to the power of the number of bits it can hold minus 1, minus 1. No, that's not a typo. Consider this. How many bits are in a byte? Eight. The Java data type byte is capable of holding 8 bits of data. So now you know the range of possible values. Let's show our work like Mr. Foss made me do endlessly back in the 10th grade. He tortured me, and now you must be tortured too. To determine the range of a byte we do this math: 2 to the power of 8 bits - 1, -1. Which is: 2 8 -1 or 2 7 -1. And 2 7 is 128, minus 1 is 127. We subtract one in order to accommodate 0, which is counted as positive. So the highest value we can represent with a byte is 127. The low range is one step easier to get: 2 8 -1 is 2 7 is 128. So a byte in Java is capable of representing the values -128 to 127.

     

Integer Types

The Java integer types are byte , short , int , and long .

byte

Occupies 8 bits or 1 byte, which is:

-2 7 to 2 7 -1 or -128 to 127

Default value of 0

Example: -17, 123

short

Occupies 16 bits or 2 bytes, which is:

-2 15 to 2 15 -1 or -32,768 to 32,767

Default value of 0

Example: 31,098, -9001

int

Occupies 32 bits or 4 bytes, which is:

-2 31 to 2 31 -1 or -2,147,483,648 to 2,147,483,647

Default value of 0

Example: 50, 2147000000, -53000

The int is probably the most commonly used integral type in Java. That's because it is often not worth trying to save memory space by using a smaller type, such as a byte . Here's why: when you perform a comparison operation or an arithmetic operation with a byte , it gets promoted to an int anyway by the runtime, then the operation is performed, and then you have an int . So it's extra work, and a little confusing. But there are many situations where you need specifically one of those types.

long

Occupies 64 bits or 8 bytes, which is:

-2 63 to 2 63 -1 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Default value of 0

Example: 9,223,372,036,854,775,807, 0L

The long takes up a good deal of memory, and so it is typically reserved for use in situations that exceed the capacity of the int , such as representing the total population of the earth, the national deficit in dollars, or the total number of hours I've spent goofing off on the Internet.

You can distinguish between a long and other integral primitive types by writing a literal L after the number value, like this: 87999065L. If you don't do that, the compiler will assume that your number is an int .