Chapter 3: Primitive Types

Java (and most object oriented languages) has two categories of variables: Objects and primitives. They behave quite differently.

The objects are used when we want to encapsulate a collection related information and actions. However, sometimes we just want to perform arithmetic operations on several numbers. In such a case, object syntax is a bit cumbersome. Instead, we want to use the more natural notation we are used to in mathematics. So java has what are called primitive types. An integer is one example. Let's see how these are used:

int width, height, area; // declares integers
width = 23; // set value of width
height = 10; // set value of height
area = width * height; // calculates area


Declaring Integers

Unlike objects, creation and declaration occur all at once.



Structure of Memory


How are Integers Stored?


Other Integer types


Floating Point Primitive types


Practice with Number Formats

1) Convert to 2's complement

a. -78 (8 bits)

b. 24 (8 bits)

c. -15 (8 bits)

d. -16 (8 bits)

e. -0 (8 bits)

f. -162 (16 bits)

2) Convert to IEEE Floating Point Format (all 32 bits)

a. -.125

b. 783

c. .0390625

For answers click here.


Other Primitive Types


next lecture