University of Chicago, CS 102, Spring 2006, Notes 3

previous  |  toc  |  next


Primitive types are the elements of Java. Java provides eight primitive types. You have already seen two: int and double.

In all the previous class examples, the fundamental building blocks of each class definition were ints or doubles. Think of int and double as elements like hydrogen or helium: as atoms of particular elements are the building blocks of all molecules, so are primitive types the building blocks of all classes.

int and double are both what we know as primitive types in Java. There is one other kind of type in Java, and that is the reference type. We will talk more about reference types later.

Typographically, primitive types are distinguished by the fact that they start with lowercase letters. Reference types, on the other hand, start (by convention) with uppercase letters.

There are eight primitive types in Java. Six of these types represent numbers. These are float and double for numbers that are not necessarily integers, and byte, short, int and long for integers. The different primitive types have different upper and lower bounds on the values they contain. These differences are summarized in the following tables.

typemin valuemax value
byte-128127
short-3276832767
int-21474836482147483647
long-92233720368547758089223372036854775807
Of these four integer types, the one that will most often do the trick is int. When you need to represent an integer, your first choice should be int, and you should only use one of the other types if you have a specific reason for so doing.

For floats and doubles, think of the numbers as being in scientific notation: something times 10 to the some exponent. When you consider the values that belong to these types, you think not only of the maximum and minimum numerical values they may assume, but how many digits of precision they provide.

typeextreme exponents for the 10significant digits
float-38 to 386 or 7
double-308 to 30815 or 16

There are two other primitive types.

typevalues
booleantrue, false
charindividual characters like 'A', '2', '$'

previous  |  toc  |  next