Sub topic 15
Choosing a Primitive Type
A primitive type is one of Java’s eight built-in types for simple values. Each type holds a different kind or range of value. A range is the set of values from the smallest to the largest that a type can hold.
Choose a type based on what the value means and what can happen to it during calculations. When in doubt, start with int for whole numbers and double for measurements.
A guide to the eight types
| Type | Good choice for |
|---|---|
| `byte` | Whole numbers from -128 to 127, especially when working with byte data |
| `short` | Small whole numbers when a storage format requires it |
| `int` | Most whole numbers, such as a count of tickets |
| `long` | Whole numbers that may be too large for `int` |
| `float` | Approximate decimal values when a format requires `float` |
| `double` | Most approximate decimal values, such as temperatures |
| `char` | A single UTF-16 code unit, such as the letter `'A'` |
| `boolean` | A choice between `true` and `false`, such as whether an event is sold out |
An int holds values from about -2.1 billion to 2.1 billion. A long has a much larger range. Choose based on the largest and smallest values you may need, not just the value you have today.
A float or double is a floating-point type: it stores an approximation of many decimal values. Use double for most measurements when small rounding errors are acceptable. Use float when another system requires it or when reducing storage for many values matters.
A worked example
A ticket seller needs to record a ticket count, a price, a temperature, a section, and whether tickets are sold out.
Save this program as TicketReport.java:
class TicketReport {
public static void main(String[] args) {
int tickets = 3;
long priceCents = 1_299L;
long totalCents = tickets * priceCents;
double temperatureCelsius = 21.5;
char section = 'A';
boolean soldOut = false;
System.out.println("Total cents: " + totalCents);
System.out.println("Temperature: " + temperatureCelsius);
System.out.println("Section: " + section);
System.out.println("Sold out: " + soldOut);
}
}Compile and run it:
javac TicketReport.java
java TicketReportThe total is 3897 cents. The price uses long, so the multiplication is also done using long. That gives the calculation more room than int, though a result can still exceed the long range. The L marks the price as a long value.
Check text and calculation needs
A char holds one UTF-16 code unit. UTF-16 is a way Java represents text using units of 16 bits. Some characters, including many emoji, need two such units. For text that may contain more than one unit, use a String, Java’s type for text, rather than a char.
Before choosing a numeric type, check intermediate results too. An intermediate result is a value produced during a calculation. Two numbers may each fit in an int, while their product does not. Use a wider type in the calculation itself, not only for the variable receiving the result.
Exercise
You multiply 50,000 tickets by 50,000 cents each. How can you calculate the total without doing the multiplication as an int?
Show answer
Use a long value in the multiplication: long total = 50000L * 50000; The result is 2,500,000,000 cents, which is too large for int.
Exercise
Which type would you choose for whether a door is open, and why?
Show answer
Use boolean because the value has two choices: true or false.