Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A data type does more than describe what a variable means. It restricts which values Java will allow in that variable and how Java stores those values. Consider a player profile for Mira. It contains whole numbers for lives, level, score, and totalPlayers, decimal numbers for accuracy and completionTime, one character for rank, a truth value for premium, and text for playerName.
byte lives = 3;
short level = 120;
int score = 125000;
long totalPlayers = 8_000_000_000L;
float accuracy = 92.5F;
double completionTime = 12.375;
char rank = 'A';
boolean premium = true;
String playerName = "Mira";The type appears before each variable in its declaration. byte, short, int, long, float, double, char, and boolean are Java's eight primitive types. Each profile variable using one of these types stores a value directly in a fixed kind of primitive storage. String is different: it is a reference type used for text, not a ninth primitive type.
The declaration gives Java a contract to enforce. lives can hold a small whole number because it is a byte. accuracy must receive a float value, rank must receive one char, and premium must receive either true or false. A value that looks suitable to you can still be rejected if its range or literal form does not match the declared type.
The four integer types all store whole numbers, but they reserve different numbers of bits. More bits provide a wider range, while smaller types use less storage. Mira's lives, level, score, and totalPlayers show the four choices from smallest range to largest.
| TYPE | WIDTH | RANGE | PROFILE VALUE |
|---|---|---|---|
| byte | 8 bits | -128 to 127 | lives = 3 |
| short | 16 bits | -32768 to 32767 | level = 120 |
| int | 32 bits | -2147483648 to 2147483647 | score = 125000 |
| long | 64 bits | -9223372036854775808 to 9223372036854775807 | totalPlayers = 8000000000 |
The type must be wide enough for the value. 3 fits in byte, 120 fits in short, and 125000 fits in int. The value 8_000_000_000 does not fit in int because it is larger than 2,147,483,647, so totalPlayers needs long.
Java also checks the literal before assigning it to the variable. An integer literal without a suffix is treated as an int when its value fits that form. The L suffix marks 8_000_000_000L as a long literal, allowing Java to represent this value and assign it to totalPlayers. The underscores only make the digits easier to read; they do not change the value.
Which declaration correctly stores totalPlayers as 8_000_000_000?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Mira's decimal values use two different primitive types. accuracy is a float, so its literal is written 92.5F. completionTime is a double, and 12.375 is a double literal by default. The F suffix tells Java to create a float literal instead of the default double literal.
A char stores exactly one character, not a word or a number written as text. rank receives 'A', where the single quotes mark one char. A boolean stores one truth value, and Java permits only true or false for premium. A decimal number cannot be used as a char, and a character cannot be used as a boolean merely because the value appears in a declaration.
float accuracy = 92.5F;
double completionTime = 12.375;
char rank = 'A';
boolean premium = true;Repair this declaration for the same profile so accuracy stores 92.5 as a float.
float accuracy = 92.5;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Mira's rank and name look similar in a profile, but their types describe different things. char rank = 'A' stores one character between single quotes. String playerName = "Mira" uses the capital S and double quotes because it stores text made from multiple characters.
The eight primitive profile variables use Java's primitive types: byte, short, int, long, float, double, char, and boolean. playerName does not use a primitive type. It holds a reference to a String object containing the text "Mira". That difference is why String begins with a capital letter and why its value uses double quotes.