1.2 Fundamental Types

The way that programming languages store information depends on the type of information being stored. There are 8 fundamental types that we'll be looking at in processing. Seven are what are known as primitive types. The last one we'll look at is what is known aa an object type but for now we will not need to worry about the distinction between the two.

Processing can work with two classes of numbers - integers and reals.

Integer Types

Depending on how large an integer you need to store, there are four types you can use. Types that can store larger values take up more computer memory. Integer values can only be made up with digits and possibly a + or - sign in front. Commas and spaces are not allowed to make larger numbers easier to read. Here is a table summarizing the types.

TypeMemory UsedRange of Values
byte8 bits -128 to 127
short16 bits -32768 to 32767
int32 bits -2 147 483 648 to 2 147 483 647
long64 bits -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807

Most of the time we will use the int type (unless we need really large numbers).

Floating Point (Real) Types

There are two types that can be used to represent floating point (numbers with decimals). The difference is both the size and the precision of the numbers you can store. The characters allowed in a real number are the same as for integers, except that you will also have a decimal point. You do not have to have a number after the decimal point (it will be assumed to be 0). You do not have to have a number in front of the decimal point (it would be assumed to be 0). These are all valid floating point constants:   -4.54      6.      .832      +333.3333

TypeMemory UsedRange of ValuesPrecision
float32 bits -3.40282347 x 1038 to 3.40282347 x 1038 at least 6 digits
double64 bits -1.8 x 10308 to 1.8 x 10308at least 15 digits

Most of the time we will use the float type (although in Java we would usually use the double type).

You can use scientific notation when working with floating point numbers in Processing. This can be necessary when dealing with very large or very small numbers. The way you write a scientific notation number in Processing is a little different from the way you write them in math or science class. Processing uses the letter E (or e) to stand in for the power of 10 and the exponent is written as a regular number. The exponent must be an integer. When you use E notation the constant will always be a floating point number. For example:

NumberScientific NotationIn Processing
123.4561.23456 x 1021.23456E2
0.637216.3721 x 10-16.3721e-1
-0.0000042-0.42 x 10-6-0.42e-6

Note that you can use either E or e. Also you can have as many digits in front of the decimal as you like. So the following are equivalent: 4321, 4321E0, 432.1E1, 43.21E2, 4.321E3, 43210E-1, 432100E-2.

boolean Type

The boolean type is used for making decisions. This type can only take on one of two possible values: true or false. We won't often make variables of the boolean type but we will often use boolean values in other ways. They will usually be used when you are making comparisons in your programs.

char Type

The char type is used to store a single character. Char constants are indicated by enclosing the character in a set of single quotes (as opposed to the double quotes we saw in section 1.1 for strings. Processing uses a system called Unicode to represent characters. Unicode uses 16 bits to represent a character so there are 216 = 65 536 characters possible. Characters from all the world's alphabets are represented using Unicode. Unicode is now superseding an older system called the American Standard Code for Information Interchange (ASCII). ASCII only used 7 bits, so it could only represent a very small subset of the characters that Unicode has. Turing uses the ASCII system.

Some examples of chars: 'A'    'z'    '$'    '2'

We run into the same kind of problem with char as we had with String. What if we want to have a single quote (or apostrophe) character? This is a problem since we use that character to begin and end a char. We'll solve it in a similar way. The apostrophe character is represented by '\'' (where the backslash is the escape character). The backslash character is represented by '\\'.

String Type

We were introduced to the String type in 1.1. It is actually a much more complicated type than all the other ones mentioned so far in this section. It is what is known as an object type. For now this won't matter but later on when we start using Strings in other ways this will become important.

Exercise 1.2

  1. A group of 4 bits is called a nibble. How many different values could be represented by a nibble?
  2. What is the smallest type of integer value that would be required to store each of the following integer values?
    1. 50000
    2. -3000000000
    3. -125
    4. 128
  3. State, with reason, which of the following are not legal integer constants.
    1. -47
    2. 123,456
    3. +55
    4. -0
    5. 987 654 321
  4. In some computer languages (although not in Processing), groups of bits can be used to represent unsigned integers whose least value is zero. In such a representation, what is the largest integer that could be stored as an unsigned integer using 32 bits?
  5. Rewrite in standard decimal form (ie. without using scientific notation)
    1. 2.94e1
    2. 0.00004E3
    3. -2e-3
    4. 26.77e-3
    5. -54E-3
    6. -.3e1
  6. Write the following using E notation
    1. 92.38
    2. 1.65
    3. -0.00951
    4. -4829
  7. Identify the type of each constant
    1. 7.
    2. -12
    3. 5E3
    4. '9'
    5. "char"
    6. true
    7. "false"
    8. '\n'