Key Takeaways
- Java is statically typed: every variable has a type known to the compiler.
- Java has eight primitive types:
byte,short,int,long,float,double,boolean, andchar. - Local variables do not receive automatic default values: assign them before reading them.
Java variables give names to values while Java types tell the compiler what kind of values those names may hold. After learning the source-code → compiler → bytecode → JVM path in OSJava.001: What Is Java?, the next foundation is learning how a Java program represents numbers, true/false values, characters, and references.
This lesson stays deliberately fundamental. It focuses on declarations, assignment, literals, primitive types, local variables, and the basic distinction between primitive and reference values. Operators, control flow, methods, classes, and collections belong in later lessons.
A Variable Has a Type, a Name, and a Value
Java is a statically typed language. A normal variable declaration identifies a type and a name, and it may also include an initial value. The official Dev.java primitive-types guide uses the same basic model: the compiler knows the variable’s type before the program runs.
int age = 25;
intis the type.ageis the variable name.25is an integer literal used as the initial value.=performs assignment.
You can also declare first and assign later:
int age;
age = 25;

The Eight Primitive Types
A primitive type is built into the Java language. Primitive values are not objects created from a class. Java defines eight primitive types.
| Type | Typical use | Example |
|---|---|---|
byte | Small signed integers | byte level = 100; |
short | Smaller-range signed integers | short year = 2026; |
int | General whole numbers | int count = 42; |
long | Larger whole numbers | long population = 8_000_000_000L; |
float | 32-bit floating point | float ratio = 1.5F; |
double | 64-bit floating point | double price = 19.99; |
boolean | true or false | boolean online = true; |
char | Single UTF-16 code unit | char grade = 'A'; |
For most beginner whole-number work, int is the normal starting point. For most beginner decimal work, double is the normal starting point. The smaller integer types and float matter, but they should be chosen for a reason rather than simply because a value currently looks small.
Primitive Values and Reference Values Are Different Categories
Primitive types hold primitive values. Reference types refer to objects or arrays. For example, String is not one of Java’s eight primitive types; it is a class in java.lang.
int score = 9000; // primitive
String player = "Amina"; // reference type
This distinction becomes important later when you study objects, methods, null, arrays, collections, parameter passing, and memory behavior. For now, remember the simplest rule: the eight language keywords in the table are primitive types; classes such as String are reference types.
Literals Are Values Written Directly in Source Code
A literal is a source-code representation of a fixed value. In the following declarations, 42, 2.5, true, and 'J' are literals.
int answer = 42;
double voltage = 2.5;
boolean ready = true;
char initial = 'J';
Some numeric literals need suffixes. A large integer literal intended as a long commonly uses uppercase L. A float literal commonly uses F. Uppercase suffixes are easier to read than lowercase forms.
long distance = 9_000_000_000L;
float efficiency = 0.95F;
Underscores can make long numeric literals easier to read. They do not change the numeric value.
Local Variables Must Be Assigned Before Use
A local variable is declared inside a method or block and stores temporary state for that execution path. Unlike fields, a local variable does not receive an automatic default value that you can safely read. The compiler requires definite assignment before use.
public class Demo {
public static void main(String[] args) {
int temperature;
temperature = 72;
System.out.println(temperature);
}
}
If the print statement came before the assignment, the code would fail to compile because the local variable may not have been initialized. This compile-time check prevents a large class of accidental reads of unknown local state.
Fields Do Receive Default Values
Fields belong to a class or object rather than to a temporary local scope. Java supplies default values for fields when no explicit initializer is provided. Numeric primitive fields default to zero-equivalent values, boolean defaults to false, char defaults to the zero character, and reference fields default to null.
This is one reason it is important to distinguish a field from a local variable. The syntax can look similar, but initialization rules differ.
Java 27 Is Still Statically Typed Even When You Use var
Modern Java supports local-variable type inference with var in allowed local contexts. That does not make Java dynamically typed. The compiler infers a concrete static type from the initializer.
var count = 42; // inferred as int
var name = "Duke"; // inferred as String
Oracle’s Java SE 27 language documentation continues to document local variable type inference alongside newer language features. Use var when the inferred type remains clear; explicit types are often easier for beginners while learning the language.
A Complete Beginner Example
public class PrimitiveTypesDemo {
public static void main(String[] args) {
byte rackUnits = 42;
short year = 2026;
int miners = 10000;
long hashes = 9_000_000_000L;
float utilization = 0.95F;
double price = 19.99;
boolean online = true;
char grade = 'A';
String site = "North Campus";
System.out.println(site);
System.out.println(miners);
System.out.println(price);
System.out.println(online);
System.out.println(grade);
}
}
The example intentionally mixes all eight primitive types with one reference type, String. Notice that each declaration ends with a semicolon. If your editor colors types, names, literals, and keywords differently, that is syntax highlighting; the colors help humans read the program but do not change Java’s rules.
Common Beginner Mistakes
- Using double quotes for a char:
"A"is aString;'A'is achar. - Forgetting an
Lon a very large long literal: the unsuffixed integer literal may be outside theintrange before assignment is considered. - Forgetting
Ffor a float literal: decimal floating-point literals are normallydoubleby default. - Reading an uninitialized local variable: the compiler rejects it.
- Calling String a primitive:
Stringis a class and therefore a reference type. - Assuming var means dynamic typing: the compiler still assigns one static type.
Java’s explicit type system is also one reason compilers, IDEs, and analysis tools can catch many mistakes before a program runs. That same ecosystem of static analysis and program understanding appears in more advanced Java tooling, including the kind of code-analysis work discussed in IBM and Red Hat Fix 400 Hidden Java Security Flaws.
Practice
- Create a file named
PrimitiveTypesDemo.java. - Declare one variable for each primitive type.
- Print at least four of the values.
- Change one
intto alongand add an uppercaseLsuffix to its literal. - Create a local variable without assigning it, try to print it, and read the compiler error.
- Change
char grade = 'A';toString grade = "A";and explain the type difference. - Replace one obvious local declaration with
varand explain what type the compiler inferred.
Knowledge Check + Answers
- How many primitive types does Java define? Eight.
- Name them.
byte,short,int,long,float,double,boolean, andchar. - Is String primitive? No.
Stringis a reference type. - What is a literal? A value written directly in source code, such as
42,true, or'A'. - Do local variables receive automatic default values? No. They must be definitely assigned before use.
- Do fields receive default values? Yes, when no explicit initializer is supplied.
- Does var make Java dynamically typed? No. The compiler infers a static type.
- Which primitive type usually stores ordinary whole numbers?
int. - Which primitive type usually stores ordinary decimal values?
double.
Elementary Review
A variable has a type and a name; an assignment gives it a value. Java’s eight primitive types cover integer numbers, floating-point numbers, true/false state, and character data. Reference types such as String are a different category. Local variables must be assigned before use, while fields receive defaults.
Next Java Lesson
OSJava.003: Expressions, Statements, and Blocks will build on these values and variables by showing how Java combines them into executable program structure.
Editor’s Note
The featured artwork is a unique 1200×630 realistic color-pencil illustration created specifically for this lesson and is not reused in the body. The separate body image is the open-source Duke mascot image from Dev.java.
BitcoinVersus.Tech content is provided for informational and educational purposes.

Leave a Reply