Primitive Data Types
Computers are all about storing and manipulating data. Java has eight primitive, or built-in, data types. The programmer can also create any number of customized abstract data types. Of the eight primitvie data types, the four types we'll use most frequently are:
the other four primitive data types are:
When Java is storing and manipulating a piece of data, it is very careful about keeping track of both its value and its type. If a program needs a place to keep track of a person's age, it might have a Java statement such as
int age;
This is a declaration statement. A declaration statement is used to create a variable of a specific type. The declaration statement does the following three things:
- Grabs a chunk of memory.
- Notes that that memory piece of memory can only store values of the indicated type.
- Gives that chunk of memory a name (in addition to the address that is automatically associated with each chunk of memory).
For example, the declaration statement
int age;
grabs four bytes of memory for the variable age, which will be used to hold only int values (whole numbers), and can be referenced by the name age.
A computer's memory is a series of data cells called bytes.

A single byte of memory is only large enough to store a single character. Four bytes of memory are required to store an integer. So, the age variable declared above might take the four bytes shown below:
Four bytes of memory is enough space to keep track of the integer values from approximately negative two billion to positive two billion. The table below shows information on the four primitive data types we'll focus on.
| Type |
Memory Used |
Range of Values |
| int |
4 bytes |
-2,147,483,648 to 2,147,483,647 (inclusive) |
| double |
8 bytes |
4.9 x 10-324 to 1.797 x 10+308 |
| char |
2 bytes |
All of the Unicode characters (represent up to 65,536 characters, such as 'a', 'b', 'c', etc.) |
| boolean | 1 bit | Two possible values: true and false. |
Looking at the table above, we see that an int variable takes four bytes of memory and can hold the values from -2,147,483,648 to 2,147,483,647 (inclusive). What if we need to store a value outside of this range? One choice is to use a double, but that is sometimes not appropriate since there are some accuracy problems with the floating point types double and float. Another choice is to use the long type, which allows whole numbers from
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (inclusive). The long type is very similar to the int type. The main difference is that the long type uses eight bytes of memory, versus four bytes for an int -- this extra space allows it to store a wider range of values.
The double type has an impressive range. The smallest possible value is 4.9 x 10-324 -- i.e., 4.9 with the decimal point moved 324 places to the left:

This is an impressively small number, but unfortunately the double values offer very few significant digits. Similarly, the largest possible double value is very big:

but again offers very few significant digits.
In a Java program you cannot write a double with scientific notation, such as 3.456 x 102. In Java this number could be written in either decimal notation
345.6
or in exponential notation
3.456e10
The value after the 'e' indicates a power of 10 (e.g., 3.456e2 means 3.456 x 102). There are several possible ways to write this same number in exponential notation, such as 34.56e1, or 0.3456e3, or even 345600.0e-3.
Expressions
An expression is a combination of operators and operands. Some typical arithmetic operators are +, -, *, / (addition, subtraction, multiplication and division, respectively). The operators take some action on the operands to yield a value. For example, in the following expression
5 + 3
the addition operator is applied to the two operands 5 and 3 to yield the value 8.
If an expression has several operators, such as
5 + 3 * 2
you need to apply the operators in a specific order. The order is determined by the precedence of the operators -- operators with a higher precedence are applied before lower precedence operators. The following precedence chart indicates the precedence of the basic arithmetic operators:
| Higher Precedence |
+ (unary) - (unary) |
* / % |
+ - |
Lower Precedence |
The unary + and - are used in expressions such as -5 or +3 (a unary operator has a single operand versus two operands for binary operators). The expression
5 + 3 * 2
has two operators: + and *. The * operator is higher than + on the precedence chart and is resolved first. The value of 3 * 2 is six, leaving
5 + 6
to resolve. The + operator is applied to give the final result of
11
If an expression contains two or more operators that occur on the same level of the precedence table, then this "tie" in precedence is resolved via their associativity. The table above table is updated to include associativity:
| Higher Precedence |
Associativity |
+ (unary) - (unary) |
right-to-left |
* / % |
left-to-right |
+ - |
left-to-right |
Lower Precedence |
|
For example, in this expression
5 * 20 / 2 * 3
the operators * and / have the same precedence. The table above indicates that these operators have a left-to-right associativity, which means that the left-most operator is performed first. The * between the 5 and 20 is left-most, and yields 100 leaving
100 / 2 * 3
The / operator is now left-most and is done next, leaving
50 * 3
which gives the final result of
150
The Modulus Operator
The modulus operator, %, gives the remainder when the operand on the left is divided by the operand on the right. For example, the value of
5 % 3
is
2
In long-hand division, this can be visualized as

The table below shows some other examples of expressions that involve the modulus operator:
| Expression |
Value |
| 10 % 3 |
1 |
| 14 % 5 |
4 |
| 10 % 2 |
0 |
| 4 % 10 |
4 |
In the last example the divisor is larger than the dividend -- the quotient is zero and the dividend becomes the remainder:
Know The Java Types
You should be able to easily identify the basic Java types. For example, you should be able to easily recognize the type of a any Java literal. For example, the type of the literal 15 is int. The type of 15.0 is a double. The type of 'a' is char. The type of true is boolean. The table below shows several literal values and their types
| Literal Value |
Type |
| 235 |
int |
| 235.0 |
double |
| 't' |
char |
| true |
boolean |
| -41 |
int |
| false |
boolean |
| 'q' |
char |
| a |
Not a literal value |
The last entry, a, is not a char because it is lacking the single quotes that identify a character literal (i.e., an 'a' versus a). It is also missing the double quotes which identify a string literal: "a". If it is not a char or a string, it must be the name of a variable (a variable name is not a literal value).
Maintaining Type
If the two operands of a binary operator have the same type, then the value of the expression will have that same type. For example, an integer plus an integer will yield an integer. In the integer expression
5 + 3
the result is the integer
8
An expression that is a double minus a double
10.03 - 1.02
the result is the double
9.01
Since the expression integer expression
3 / 2
will yield an int result, the value is
1
and not 1.5 (1.5 is not an int value).
However
3.0 / 2.0
does yield 1.5 (a double divided by a double will yield a double).
Here are some examples of expressions, their values, and their type:
| Expression |
Value |
Type |
| 5 / 3 |
1 |
int |
| 5 % 3 |
2 |
int |
| 5.0 / 3.0 |
1.6666 |
double |
| 7 / 2 |
3 |
int |
| 7.0 / 2.0 |
3.5 |
double |
Mixed Types in an Expression
Java does not like to have mixed types on an operator. If such a situation occurs, Java has a predetermined order for the primitive data types -- if two different types share a binary operator then Java will cast the
lower of the two types to the highest type on that operator. The order is:
| Higher |
double |
float |
int |
Lower |
.For example, the expression
7 / 2.0
has both an int and a double operand. Since double is a higher type than int, the 7 is temporarily converted to the double 7.0 to allow the division to take place, giving 7.0 / 2.0 -- now we have a double divided by a double to yield the double result of 3.5. Changing the type of the lower operand to the type of the higher operand is referred to as promotion.
Casting
A value can be manually changed to another type via the casting operator. The casting operator is created by placing the name of a type between parenthesis, such as
(int)
The casting operator is a unary operator which is placed just to the left of its operand, as shown below
(int) 5.97
The value of the above expression is 5 (i.e., the integer five). Note that the (int) casting operator does not round the value, it truncates it (i.e., just chops off everything to the right of the decimal point).
Here are some more example of the casting operator. Note that the casting operator can be used to perform both promotions (change the type to a higher type) or demotions (change the type to a lower type):
| Expression |
Value |
| (int) 6.1 |
6 |
| (int) 17.888 |
17 |
| (double) 45 |
45.0 |
| (double) 2 |
2.0 |
| 2 + (double) 2 |
4.0 |
| 2 + (int) 2.0 |
4 |
| (double) 7 / (double) 2 |
3.5 |
| (int) 7.0 / (int) 2.0 |
3 |
Use jGRASP to Help Your Understanding
If you are curious about the value of an expression, just enter it in a simple Java program and run the program under jGRASP. For example, to verify the last expression in the above table, paste the following program into jGRASP and look at the output:
public class VerifyIt
{
public static void main (String [] args)
{
System.out.println( (int) 7.0 / (int) 2.0 ); // Put the expression here
}
}
On a scrap piece of paper write your answers to the following questions - then use jGRASP to verify your answers:
| Expression |
Value |
| 1.6 + 1.7 |
|
| 1.6 + (int) 1.7 |
|
| (int) 1.6 + (int) 1.7 |
|
| (int)(1.6 + 1.7) |
|
|