Home Topic 4

Data Types

Primitive Data Types

A simple definition of programming says that it is operations, which are performed on things. In other words, there are actions (functions) and there are things upon which those objects work. Those things take on different forms, or better put "types" that we use to distinguish between different forms of data. We call these kinds of data types. Note you can talk about "Java data types", or just "Java types"; both mean the same thing.

In the very beginning of learning about computers, we said that the kind of data computers work with, is, generally words, numbers, colors and sounds. For now we do not need to worry about colors and sounds, which are represented in slightly more complex ways, and rather take a look at the kinds of words and numbers that Java, in our case, works with. We'll call these primitive data types, since these are the basic building blocks of the language upon which other objects (including colors and sounds) are built.

These are Java's data types for words, and letters:

String*, char

And these are Java's data types for numbers:

int, byte, short, long, float, double

Plus, there's one other primitive data type:


(*String is actually not completely primitive, but it can behave that way)

So, in terms of the "things" in Java which we build, above are all of the fundamental building blocks.


String is the data type used for working with words in Java, and many other programming languages. Note that compared to the other data types, it is the only one which is capitalized. That is because, as noted above, it is actually not a primitive, it is a "class" of things, and as we'll see later, in Java, classes are capitalized.

A String is a string (or chain) of characters. The word string is used in this sense other places, such as a "string of pearls". Examples of Strings, in Java, therefore include any combination of characters, such as "hello", "hello world", "#I Love Java", "123,456", "@", and even the empty string "".



char is the Java type for an individual character. Some people pronounce it like chocolate, and others pronounce it like car; both are fine. Examples of chars in Java include all of the individual characters on a keyboard, like 'a', 'A', '1', '@', and '+'. You'll note that '@' was included as an example of a String too, so an individual character can be either a String or a char.

One thing to note is that when working with Strings and chars in Java, double quotes are used for Strings, and single quotes are used for chars. So "hello", and "@" are Strings, and 'h', and '@' are chars.


int, byte, short, long

int, short, and long are the Java data types for integer numbers, i.e. whole numbers, numbers without decimal places. The are all what is referred to as "signed" types, meaning that they can store both positive and negative values. Examples include 123, -456, and 78910.

The reason there are three kinds of integer data types is to give you the option of using data types that take up less computer memory, if your number values are not too big. Here's the range of values for each:

byte: -128 to +127
short: -32,768 to +32,767
int: -2,147,483,648 to +2,147,483,647
long: -9,223,372,036,854,775,808 to+ 9,223,372,036,854,775,807.

The amount of memory needed for each is, byte: 8 bits, short: 16 bits, int: 32 bits, and long: 64 bits.

So the point is, that if you are only dealing with values less than 100, for example, why use 64 bits to store each of those numbers, if 8 bits will suffice.

The most used integer type in Java is by far and away int.


float, double

float and double are the Java real number types, i.e. numbers with decimal places. They, too, are signed types, able to store both positive and negative values. Examples include 123.456, -789.012, 0.000003, and 0.0.

With real numbers, having two different types is not so that we are able to store values of larger values per se, rather numbers which are more accurate, i.e. have more significant digits. Here is the range of values for each:

float: 3.40282347 x 10 ^ 38, 1.40239846 x 10 ^ -45
So note in particular how many decimal places there are, and even more with the type double:

double: 1.7976931348623157 x 10 ^ 308, 4.9406564584124654 x 10 ^ -324

The amount of memory needed for each is float: 32 bits, and double: 64 bits.

Notes about the names float and double:

float is called float because it is a number for which the decimal point can float back and forth. The computer just keeps track of the numerals used in the number and the position of the decimal place. To a computer, therefore, 12345678.1, and 1.2345678 take up exactly the same amount of memory.

And double is called double, because, as seen above, it takes up double the amount of computer memory.



You'll recall that the two most fundamental things that the CPU of a computer can to is do arithmetic and perform logical (comparison) operations - in the Arithmetic Logic Unit (ALU). To perform comparisons, an operator is needed to store whether or not two pieces of data are the same or not. The Java type that exists for this purpose is called boolean; it stores either true or false. And that's it. So for examples of booleans, there are only two: true, and false.

boolean is named after the branch of math developed by George Boole, which became known as Boolean Algebra.

Even though only one bit would be required for a boolean, it actually takes 8 bits, i.e. one byte, to store.


Declaration of Primitive Types

The way you declare the existence of a new primitive type in Java is to type the data type, and then choose and type a variable name, like this:

int i;
double temperature;
boolean b;

Assigning values to Primitive Types

In Java, we use the "assignment operator", which is the equals sign character to assign a certain value to a certain variable, like this:

i = 943;
temperature = 30.6 ;
b = false;

Note that declaration and assignment of primitive variables can be done in the same line, like this:

int k = 943;
double temperature = 30.6
boolean b = false;



Object Data Types

All other types of data used by Java are "non-primitive" "object" types. An object type is made up of a bunch of primitive data types and/or other object types.

A good place to start to understand this is to consider, as mentioned above, that String is actually a non-primitive, though it can act like a primitive. In fact, the way it is structured is that it's an "array" of characters, i.e. a bunch of characters.

Another object may, for example, be made up of a String, a boolean, and two ints. Say, for example, a Student object - made up of String name, boolean isFullIB, int age, and int grade.

Object types are also called "reference" types, because at the memory level, they are literally a 64 bit address, which points to the data which makes up the object. So if we had a Student object s, s is a shortcut for a memory address like #4444FFFF, and that #4444FFFF is where the name, isFullIB, age and grade are stored, elsewhere, at that address, in computer memory. There will be much more detail on all of this later on in the course.

Declaring Object Types

You know you are working with object data types two ways; when you declare them, and when you use them.

Here's the way a declaration of a new object looks in Java:

Student s = new Student( );

And here's the way using an object looks in Java: