Primitive data types
The purpose of data types is that they allow the compiler to know how big
a variable is and what kinds of operations are valid.
- integers: int, byte,
- reals: double, float
- logical: boolean
- character: char
Literals: A literal is an explicit data value used in a program
such as 5.
- Integers have no fractional part.
- Reals have a fractional part (which may be 0 or implicit in exponential
- The size of each data type is the same for all hardware platforms
(a real advantage for Java).
- All numeric types are signed, meaning that both positive and negative
values can be stored in them.
- Java assumes all integer literals are int, unless explicitly
stated. For example, you can append an L or l to the
end of the value to indicate that it is a long (e.g., 45L
- All real literals are assumed to be of type double.
- ASCII character set - uses seven bits per character, providing room
to support 128 (27) different characters. Using the full
eight bits extends ASCII to 256 (28) different characters.
- Java uses the Unicode character set, which uses 16 bits per character,
supporting 65,536 (216) unique characters.
- The character literals are single letters enclosed in single quotes
(e.g., 'A' or 'a'). The special ones have a backslash in front,
('\b'), tab ('\t') or newline ('\n').
- Upper case and lower case letters are different characters.
- The boolean data type has only two valid values: true
- A boolean value cannot be converted to any other data type, nor can
any other data type be converted to a boolean value.