Primitive Types in Java
An identifier is a sequence of one or more characters used to name classes, variables, and subroutines.
A Name That Java Refuses
Every Java program needs names. A program refers to classes, variables, and subroutines by name, and those names are called identifiers. It is tempting to think that any readable text can serve as a name, but Java applies rules to the characters and structure of every identifier. This article follows a trace-and-check approach: you will test candidate names against the rules, see how each character is judged, and then look at the eight built-in primitive types that give variables their kind of value.
What do you think happens?
Is 'my score' a legal Java identifier for a variable?
Reveal answer
No, because identifiers cannot contain spaces
An identifier is a sequence of letters, digits, and underscores. A space is not one of the allowed characters, so 'my score' is rejected even though it reads naturally. Writing myScore or my_score would be valid.
What an Identifier Is
An identifier is a sequence of one or more characters used to name classes, variables, and subroutines.
The identifier is the label the programmer chooses. When the program needs to find a variable, class, or subroutine, it looks up that label. A good identifier therefore has two jobs: it must satisfy Java's rules so that the compiler accepts it, and it should be meaningful to the person reading the code.
Tracing Identifier Characters
The diagram below traces the candidate identifier total_2 one token at a time. Read each position and ask whether the character is allowed there. The first position is checked against the starting-character rule, and every later position is checked against the broader rule for letters, digits, and underscores.
Now trace the same check for 2total. The first character is a digit, which fails the starting-character rule, so the whole identifier is rejected no matter what follows. Changing only the position of the digit changes the verdict, which is why the rule is about structure as well as character set.
The Eight Primitive Types
Primitive types are Java's eight built-in categories for storing basic values. Choosing a primitive type is not merely choosing a label for a variable: the type determines what kind of value the variable can hold and, for several types, how much memory is used to represent that value.
The eight types are byte, short, int, long, float, double, char, and boolean. They fall into four families. The integer family holds whole numbers and contains byte, short, int, and long. The real-number family contains float and double. The char type holds one Unicode character, and the boolean type holds true or false. The diagram groups them by family so you can see the full set at a glance.
Integer Types and Their Growing Ranges
All stored data is represented as binary numbers made from bits, where each bit is a zero or a one. Eight bits make one byte. Java's four integer types use different numbers of bytes, so they can represent different finite ranges of whole numbers. The important pattern is the progression: byte, short, int, and long each use more bytes than the one before, and more bytes provide more possible integer values. You do not need to memorize every endpoint to understand this design.
Choosing an integer type for a population count
A program must store the number of visitors to a museum in one year. Which integer type should it use, and why does the choice depend on the finite range?
- 1Identify the kind of value
The number of visitors is a whole number, so an integer type is the right family. Real-number types are not needed.
- 2Consider the default recommendation
The source recommends int for ordinary integer data in most situations, so int is the starting point.
- 3Check the range against the data
Each integer type can only represent a finite range. The program must confirm that the largest plausible value fits inside the range of int. If a value could exceed that range, a wider type such as long would be the safer choice.
- 4Avoid choosing by size alone
Picking byte because it uses less memory would be risky here, since a small range can be exceeded by a real count of visitors.
Use int for typical counts, and move to long only if the range check shows that int could be exceeded.
Float Versus Double
The real-number family contains float and double. Both represent real-number values, but they differ in three ways: storage size, approximate maximum range, and significant digits. Significant digits describe how many meaningful digits can be retained approximately. In general, double offers more of each than float, at the cost of more storage. The comparison below summarizes the contrast in the terms the source uses.
| Property | float | double |
|---|---|---|
| Family | Real-number type | Real-number type |
| Storage size | Smaller | Larger |
| Approximate maximum range | Narrower | Wider |
| Significant digits | Fewer | More |
Relative comparison of float and double, as described in the source
Why Finite Storage Only Approximates
Every primitive value uses a finite number of bits, and that finite representation creates a boundary. An int cannot represent every possible integer, and float and double cannot represent every mathematical real number exactly. The value 0.1 is a useful example: it is a simple decimal, yet a fixed number of bits can only store an approximation of it. The stored bits represent a nearby value, so arithmetic on such values can show small inaccuracies.
A finite number of bits can represent only a finite set of values. Any mathematical value outside that set, or between the set's representable points, can only be approximated.
Mistakes Beginners Make with Identifiers and Types
Common mistake Using spaces inside a name
What it looks likeint my score = 10;Why it failsIdentifiers cannot contain spaces, so the compiler reads this as separate tokens and rejects it.
Do this insteadUse int myScore = 10; or int my_score = 10;Common mistake Starting a name with a digit
What it looks likeint 2ndPlace = 2;Why it failsThe first character of an identifier must be a letter or an underscore, and a digit is not allowed there.
Do this insteadUse int secondPlace = 2;Common mistake Assuming capitalization does not matter
What it looks likeint count = 1; System.out.println(COUNT);Why it failsCount, count, and COUNT are different identifiers, so COUNT was never declared.
Do this insteadUse the same capitalization everywhere the name appears.
Common mistake Using a reserved word as a name
Why it failsReserved words are reserved for the language itself, so the compiler cannot treat them as programmer-chosen names.
Do this insteadChoose a descriptive name that is not a Java keyword.
Common mistake Expecting float or double to store decimals exactly
Why it failsA finite number of bits can only approximate some real values, so a stored value may differ slightly from the value written in the source.
Practice Choosing Names and Types
For each candidate name, decide whether it is a valid Java identifier: studentName, 3Dmodel, total-score, _temp, and class. Then for a variable that stores a person's age in whole years, choose an integer type and justify it using the range idea from this article.
Hints
- Check the first character first, then scan the rest for spaces or characters other than letters, digits, and underscores.
- A hyphen is not an allowed character. Think about which word is a reserved word in Java.
- The source recommends int for ordinary integer data, so ask whether a plain int range would be a sensible starting point for an age.
Key Takeaways
- An identifier is a sequence of letters, digits, and underscores that names a class, variable, or subroutine; it must begin with a letter or underscore, cannot contain spaces, cannot be a reserved word, and is case sensitive.
- Java has eight primitive types: byte, short, int, long for integers; float and double for real numbers; char for one Unicode character; and boolean for true or false.
- The four integer types use increasing numbers of bytes, so each step up provides more possible integer values, and int is the recommended default for ordinary integer data.
- Float and double differ in storage size, approximate range, and significant digits, and because every primitive value uses finitely many bits, some mathematical values can only be approximated.