Variables and Primitive Types
Declaring variables in a statically typed language, and the eight primitive types Java builds everything on top of.
2 min read
Every variable in Java has a type, and that type is fixed for the variable's lifetime — this is what "statically typed" means in practice. You declare the type up front, and the compiler enforces it everywhere that variable is used.
int age = 30;
double price = 19.99;
boolean isActive = true;Trying to assign a String to an int variable, for instance, is a compile-time error, not something you discover when the code eventually runs.
The eight primitive types
Java has exactly eight primitive types — the basic building blocks that aren't objects and don't need new:
| Type | Holds | Example |
|---|---|---|
| byte | 8-bit integer | byte b = 100; |
| short | 16-bit integer | short s = 30000; |
| int | 32-bit integer | int count = 42; |
| long | 64-bit integer | long big = 10000000000L; |
| float | 32-bit decimal | float f = 3.14f; |
| double | 64-bit decimal | double d = 3.14159; |
| char | single 16-bit character | char c = 'A'; |
| boolean | true or false | boolean flag = false; |
int and double cover the vast majority of everyday code — reach for long only when a value might exceed roughly 2.1 billion, and float rarely (it's less precise than double and mostly used to save memory in large arrays). Note the trailing L on long literals and f on float literals — without them, Java assumes int and double respectively, and a large literal without L will fail to compile.
Declaring vs. initializing
A variable can be declared without a value and assigned later, but it must be assigned something before it's read:
int total; // declared, no value yet
total = 100; // now initialized
int result;
System.out.println(result); // compile error: variable might not have been initializedThis is the compiler protecting you from a class of bugs common in languages that silently default uninitialized variables to zero or garbage.
var and type inference
Since Java 10, var lets the compiler infer the type from the right-hand side, saving you from repeating an obvious type:
var name = "Ada"; // inferred as String
var count = 10; // inferred as intvar doesn't make Java dynamically typed — the type is still fixed at compile time, it's just not written out. It's best reserved for cases where the type is obvious from context; overusing it on ambiguous expressions can make code harder to read at a glance.
Naming conventions
Java variable names use camelCase by convention — firstName, not first_name or FirstName. Constants (declared with final, covered later) are conventionally ALL_CAPS_WITH_UNDERSCORES. These are style conventions the compiler doesn't enforce, but every Java codebase you'll encounter follows them, and deviating makes your code look foreign to other Java developers.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.