Video lecture
Watch on YouTube(opens in a new tab)Sub topic 4
Whole Numbers: byte, short, int, and long
Java has four types for whole numbers: byte, short, int, and long. A whole number has no decimal part. Examples include -12, 0, and 42.
A type tells Java what kind of value a variable can hold. A variable is a named place for a value. Each whole-number type has a different range, or set of values it can hold. Choosing a type with a large enough range helps your program store the values it needs.
This chapter assumes you have seen the basic structure of a Java program, including a main method.
Four types and their ranges
The four whole-number types are primitive types. A primitive type is a built-in Java type for a simple value. All four types can hold negative numbers, zero, and positive numbers.
A bit is a unit that can be either 0 or 1. Each type uses a fixed number of bits to represent its values. More bits allow a wider range.
| Type | Bits | Smallest value | Largest value |
|---|---|---|---|
| `byte` | 8 | -128 | 127 |
| `short` | 16 | -32,768 | 32,767 |
| `int` | 32 | -2,147,483,648 | 2,147,483,647 |
| `long` | 64 | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |
The commas in this table make large numbers easier to read. Do not put commas inside a number in Java code.
Notice that each range has room for one more negative value than positive value. For example, a byte can hold -128, but it cannot hold 128.
Eight bits provide 256 possible patterns of zeros and ones. Java uses those patterns to represent every whole number from -128 through 127, including zero.
Declare whole-number variables
To declare a variable is to give Java its type and name. To initialize it is to give it its first value. You can do both in one line:
int count = 3;
Here, int is the type, count is the name, and 3 is the first value. The = sign assigns the value on the right to the variable on the left.
The number 3 is a literal: a value written directly in the program. The semicolon marks the end of the statement.
You can later give the variable a new value. This is called an assignment:
count = 4;
The new value must fit the variable’s type. For example, byte small = 127; is valid, but byte small = 128; is not. The second value is outside the byte range.
A worked example
This program declares one variable of each whole-number type. It prints their values, then updates and prints the int variable.
Save it in a file named WholeNumbers.java:
class WholeNumbers {
public static void main(String[] args) {
byte small = 10;
short medium = 1000;
int count = 3;
long population = 10000000000L;
System.out.println(small);
System.out.println(medium);
System.out.println(count);
System.out.println(population);
count = count + 1;
System.out.println(count);
}
}A compiler is a tool that checks Java code and prepares it to run. From a terminal in the folder containing the file, compile and run the program:
javac WholeNumbers.java
java WholeNumbersThe output is:
10
1000
3
10000000000
4Look at the last two lines of the program. In count = count + 1;, Java reads the current value of count, adds 1, and assigns the result back to count. Since count was 3, the next print shows 4.
The population value is ten billion. That is greater than the largest int, so the program uses long. The L after the number is important; the next section explains why.
Writing a long value
For an ordinary decimal whole-number literal, Java uses the int type unless you add L or l at the end. That letter is called a suffix: a mark added to a literal to indicate its type.
Use an uppercase L because lowercase l can look like the digit 1.
For example, long population = 10000000000L; is valid. The L tells Java that the literal is a long.
If you write long population = 10000000000; without the suffix, the compiler rejects the literal because ten billion is too large for an int. Declaring the variable as long does not change the literal’s type.
For a smaller value, the suffix is optional when assigning to a long. Both long distance = 1000; and long distance = 1000L; are valid. The first literal fits in an int, and Java can place that value in a long variable.
Choosing among the four types
For many ordinary whole-number values, start with int. It has a wide range and is a common choice for counts, ages, and positions.
Use long when a value may go beyond the int range. For example, a count of ten billion needs a long.
Use byte or short when the smaller range is suitable and you have a reason to use a smaller type. For example, a program working with a large collection of small values may use them to save space. Check both the smallest and largest possible values before choosing.
A type’s name alone does not tell you which one to choose. Ask what values the variable must hold. A byte can store a small negative reading as well as a small positive one. An int can store a negative count, even if a particular program never expects one.
Check values against the range
Suppose a variable will hold a temperature reading expressed as a whole number, and you know every reading will be from -50 through 60.
- Check the smallest value:
-50is greater than-128. - Check the largest value:
60is less than127. - Both ends fit in the
byterange, sobytecan hold every whole number between them.
For example, byte reading = -50; is valid. So is byte reading = 60;.
Now suppose a different reading could reach 500. That does not fit in a byte, but it does fit in a short or an int. For a single ordinary variable, int is a straightforward choice.
Common mistakes
Picking a type that is too small
The declaration short score = 40000; is rejected because 40,000 is above the short maximum of 32,767. Use a type with a range large enough for the value, such as int.
Similarly, changing a byte variable from 127 to 128 with the statement small = 128; is not allowed. The new value must fit too.
Forgetting the `L`
The declaration long stars = 5000000000; is rejected: the literal is too large for an int. Write long stars = 5000000000L; instead.
Using commas in a number
The declaration int count = 1,000; does not write the number one thousand in Java. Write int count = 1000; instead.
Exercises
Exercise
Which types can hold the value -200: byte, short, int, or long?
Show answer
short, int, and long can hold -200. A byte cannot, because its smallest value is -128.
Exercise
A program needs to store 3000000000. Write a valid declaration for a variable named total.
Show answer
long total = 3000000000L; The value is larger than the int maximum, so it needs a long literal and a long variable.
Exercise
A variable starts at 100 and may later reach 200. Can it be a byte? Why or why not?
Show answer
No. Although 100 fits, 200 is greater than the byte maximum of 127. An int can hold both values.