Java (and most object oriented languages) has two categories of variables: Objects and primitives. They behave quite differently.
The objects are used when we want to encapsulate a collection related information and actions. However, sometimes we just want to perform arithmetic operations on several numbers. In such a case, object syntax is a bit cumbersome. Instead, we want to use the more natural notation we are used to in mathematics. So java has what are called primitive types. An integer is one example. Let's see how these are used:
int width, height, area; // declares integers
width = 23; // set value of width
height = 10; // set value of height
area = width * height; // calculates area
Unlike objects, creation and declaration occur all at once.
A bit is the basic unit of information storage in the computer. A single bit can take on the value of 0 or 1. Thus, a single bit can represent at most two pieces of information.
Computer memory is composed of a long list of bits.
Bits are grouped in bytes. A byte is a consecutive set of 8 bits. A byte can represent 28=256 different pieces of information.
Most information in the computer is stored in memory locations whose size is some multiple of bytes. See description below for integers and floating point numbers.
Binary numbers - discussed in chapter 0, pages 6-8
The data type int is stored in 4 bytes or 32 bits (1 byte = 8 bits).
This means that there are 2 32 @ 4 billion unique numbers that can be represented.
If we insist that there are equal numbers of positive and negative numbers than this means we can represent integers in the range of about -2 billion to 2 billion.
See Table 3.1 on p. 86
There are several integer types that differ by size:
Which do you use?
Integers are stored precisely. If the size of an integer is larger than the space allowed, an error will occur (the compiler may not tell you - you will just get strange output)
If you are working with numbers that have a fractional component or if we are working with integers that don't fit long (unlikely) then we can use floating point numbers.
For floating point numbers, if the number of significant digits is larger than the number of bits available, the number is simply rounded to the closest value that can be represented.
This means that number may not be stored exactly. However, this loss of precision is often not a problem. We refer to the error as roundoff-error.
Floating point numbers (i.e. numbers with a decimal point) are represented using what is called the IEEE format:
Practice: What is the following floating point number in base 10?
0001 0001 0100 1000 0000 0000 0000 0000
Floating point types:
1) Convert to 2's complement
a. -78 (8 bits)
b. 24 (8 bits)
c. -15 (8 bits)
d. -16 (8 bits)
e. -0 (8 bits)
f. -162 (16 bits)
2) Convert to IEEE Floating Point Format (all 32 bits)
a. -.125
b. 783
c. .0390625
For answers click here.
boolean: Boolean values are true or false. E.g.
char: represents a single character on the keyboard. E.g.
String: represents a sequence of characters on the keyboard. E.g.
WARNING: Strings use double quotes (eg "a") and chars use single quotes (eg 'a'). The values "a" and 'a' are VERY different.