Primitive Types
Goals
- Understand and identify the primitive types.
- Recognize implicit and explicit casting.
Concepts
- cast
- double precision
- floating point
- Institute of Electrical and Electronics Engineers (IEEE)
- narrow
- primitive type
- signed
- unsigned
- widen
Language
booleanbytedoublefinalfloatintlongshort
Preview
| Name | Description | Examples |
|---|---|---|
byte | integer | -123, 0, 55, 222 |
short | integer | -12345, 0, 5, 6789 |
int | integer | -123456789, 0, 5, 123456789 |
long | integer | -123456789012345L, 0L, 5L, 123456789012345L |
float | floating point | -1.23f, 0.0f, 5.0f, 1.23f, 123e4f |
double | floating point | -123456789.0, 0.0, 123e4, 123456789.0 |
boolean | Boolean logic | true, false |
char | character | 'a', 'A', '?', '\'', '\u092E' |
Lesson
The two Java types you have used so far, byte and int, are both classified as primitive types because they are represented directly by some series of bits in computer memory. A primitive type actually sets aside a number of bytes, based upon the size of the type. Here are the primitive types Java recognizes:
| Name | Description | Bytes | Min | Max | Examples |
|---|---|---|---|---|---|
byte | integer | 1 | -128 | 127 | -123, 0, 55, 222 |
short | integer | 2 | -32,768 | 32,767 | -12345, 0, 5, 6789 |
int | integer | 4 | -231 | 231-1 | -123456789, 0, 5, 123456789 |
long | integer | 8 | -263 | 263-1 | -123456789012345L, 0L, 5L, 123456789012345L |
float | floating point | 4 | complicated | complicated | -1.23f, 0.0f, 5.0f, 1.23f, 123e4f |
double | floating point | 8 | complicated | complicated | -123456789.0, 0.0, 123e4, 123456789.0 |
boolean | Boolean logic | platform-dependent | n/a | n/a | true, false |
char | character | 2 | U+0000 | U+FFFF | 'a', 'A', '?', '\'', '\u092E' |
Integer Types
There are four integer types: byte, short, int, and long. They can hold positive or negative whole numbers, but cannot represent fractions of a number. Make sure you don't try to assign a number larger than the maximum or smaller than the minimum supported by the type. By default Java considers an integer literal to be an int unless you place an L or an l after it, making it a long.
final byte foo = 123; //uses 1 byte
final short bar = 123; //uses 2 bytes
final int example = 123456; //uses 4 bytes
final long test = example; //uses 8 bytes
Floating Point Types
The types float and double are primitive types that allow fractional parts of a number. The fractional part is indicated by a decimal point. The name “float” means floating point, which refers to certain way computers represent the whole and fractional portions of the value in memory. The name “double” means double precision, which indicates that more bytes are used internally and therefore a more precise representation of the fractional part is possible. By default Java considers a floating-point literal to be an double unless you place an F or an f after it, making it a float.
final float foo = 1.23f; //uses 4 bytes
final double bar = 123456789.123; //uses 8 bytes
Character Type
The char type is conceptually different than the other types. Technically it holds an integer value; you can assign a byte or a short to it, for example. But the value doesn't simply represent an integer; the value represents a “Unicode code point”, which you will learn about in later lessons. Essentially the integer value held by char represents a letter, digit, or some other symbol, and that's how you usually should think of char; the fact that it stored as an integer value “behind the scenes” is a secondary thought most of the time.
The literal representation of a char is simply the letter or other symbol inside single quote, such as 'G'. The backslash character is special; it represents an escape sequence for entering control characters and other special characters. Popular escape sequences are '\t' for tab, '\n' for a newline character, '\'' for a single quote, and '\\' to represent the escape character itself. You can also use the escape sequence '\uXXXX' (where X is some hexadecimal digit) to insert a single Unicode code point.
final char foo = 'A'; //uses 2 bytes
final char newline = '\n'; //uses 2 bytes
Boolean Type
The boolean type represents a simple value of true or false.
Type Casting
When moving a value between variables or using it in expressions, the type of the value can change. When assigning an int value to a long variable, for example, Java will implicitly widen the value. But going in the other direction has the potential of losing information, as a smaller type cannot hold all the values that a larger type can. In that case you must explicitly ask Java to narrow the value by using a cast. An explicit cast is performed by placing the destination type in parenthesis before the value to be cast.
long to an int.final int foo = 5;
final long bar = foo; //implicit widening
final int risky = (int)bar; //cast for explicit narrowing; be careful!
Review
Summary
| Bytes | Bits | Signed | Min | Max | |
|---|---|---|---|---|---|
byte | 1 | |........| (8) | yes | -128 | 127 |
short | 2 | |........ (16) | yes | -32,768 | 32,767 |
int | 4 | |........ (32) | yes | -231 | 231-1 |
long | 8 | |........ (64) | yes | -263 | 263-1 |
float | 4 | |.-....... (32) (1+8+23) | yes | complicated | complicated |
double | 8 | |.-....... (64) (1+11+52) | yes | complicated | complicated |
boolean | platform-dependent | platform-dependent | n/a | n/a | n/a |
char | 2 | |........ (16) | no | U+0000 | U+FFFF |
Gotchas
- Never, ever use
floatordoubleto represent money, or any other values that must be exact. The floating point values in Java are estimates and are appropriate for calculations of scientific numbers, not exact quantities of things. - The backslash
\character is used to indicate “escape sequences” such as'\t', which represents a tab. Therefore if you want to represent the backslash\character itself don't forget to double it:'\\'.
In the Real World
- You will almost always use the
inttype for whole numbers. Use alongonly if the value it will contain is potentially very large. - You don't see
shortused much anymore—why would you, when the registers in most CPUs nowadays can hold more than than ashort? - Use
doublenormally, and only usefloatif storing large quantities of values when space is a premium (e.g. a million sensor measurements with limited memory). - For individual variables, there isn't much point of using
float, because internally the number of floating point bits used by the CPU is larger anyway.
Think About It
- Does the value I'm using represent an exact quantity? If so, a floating point type must not be used. If the exact number of decimal points is known, perhaps you could store a whole number value representing the fraction—representing cents instead of dollars, for example, if storing currency values.
Self Evaluation
- When would Java widen an
intinto along? What would the underlying bytes look like in memory? - What types should never be used to represent money? What might you use instead?
- What Java integer type is unsigned?
- What Java type would you use to represent the values
0–255, that is the full value range of eight bits?
Task
Create a program that solves the following problems, printing out the answers. Do not use loops, and use as few literals as possible. Verify your answers with a calculator.
- Assign the integer value
5to a floating point variable and print it. - Assign the value
0.1to adoublevariable and print it. - Polly had
$1.00and gave$0.70to Perry. How much money does Polly have left? - How much farther from the sun is Pluto than Earth? Use scientific notation when providing literal values, and perform your calculation in kilometers.
- What is the slope of a line with a rise of
456and a run of789? See Using Slope and y-Intercept to Graph Lines. - If
17people are in a room and5of them leave, what percentage of the original group is still in the room? This calculation will be discussed more in an upcoming lesson.
Create a compressed archive of your entire project directory and send it to your teacher as usual.
See Also
- Primitive Data Types (Oracle - The Java™ Tutorials)
- The Floating-Point Guide
- Type Casting (Study Tonight)
References
- Characters (Oracle - The Java™ Tutorials)
- What Every Computer Scientist Should Know About Floating-Point Arithmetic (Oracle)
- Why 0.1 Does Not Exist In Floating-Point (Rick Regan)
Resources
Acknowledgments
- Primitive types tables modified from the table in The Java™ Tutorials (Oracle).