Primitive Types

What Are They?

These are the types we give to variables, so that the computer knows what 'type' of information we are storing inside of the variable. We must give a variable a type when the variable is declared. The computer allocates memory (bits) for the variable based on which type it is. Now that you have this space in memory when you store something there it will not get lost, the computer knows where to go to find the information. Java specifies for us the size of each of the primitive types. This helps the Java language be portable, on any machine, anywhere the size of these types are always the same.

Logical Data Type
Type Range Size Variable Declaration
boolean true, false N/A loop boolean loop;
Numerical Data Types
Type Range Size Variable Declaration
byte -128 to 127 8 bits bits_8 byte bits_8;
short -32,768 to 32,767 16 bits TALL short TALL;
int -2 billion to 2 billion 32 bits sum int sum;
long -9 quintillion to 9 quintillion (huge) 64 bits mile long mile;
float -3.4 e+/-38 to 3.4 e+/-38 32 bits pi float pi;
double -1.7 e+/-308 to 1.7+/-308 64 bits stuff double Stuff;
Character Data Type
Type Range Size Variable Declaration
char Single (Unicode) Characters 16 bits letter char letter;

Literals

A literal is a primitive type value. It is the actual number, character, or boolean value that is stored in the variable. When the initial assignment is made to a variable, the variable is 'initialized', you can give the variable a value from another variable or you can give the variable the specific value or literal value you wish it to have. In the following examples false, 'A', 35, and 7.83 are the literals.
boolean loop = false;
char letter = 'A';
int sum = 35;
float pi = 7.83F;
Notice two things, first, I have declared the variables and then initialized them within the declaration. Second, I have used the letter 'F' at the end of the literal 7.83. This means that when the computer sees the number 7.83, it will see it as a float, not as a double. Java defaults to storing all literal decimal numbers as double, unless it is specified that they should be float. If you were to leave off the 'F' and try to compile the program it would not compile successfully. A decimal literal can be declared as float by appending either 'f' or 'F'. In the same manner we can declare a decimal literal to be double by appending either 'd' or 'D'. Even though this is the default it is a good practice to append the 'D'.