OSJava.002: Primitive Types and Variables — byte, short, int, long, float, double, boolean, char, Literals, and Local Variables

Diverse students learning Java primitive types and variables on a laptop in a colored-pencil classroom scene

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, and char.
  • 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.

LinkedIn Learning introduces Java primitive data types and shows how values are stored in variables.

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;
  • int is the type.
  • age is the variable name.
  • 25 is an integer literal used as the initial value.
  • = performs assignment.

You can also declare first and assign later:

int age;
age = 25;
Duke, the open-source Java mascot, holding the Star7 handheld device
Duke, the open-source Java mascot, shown with the Star7 device from Java’s early history. Source: Dev.java / Oracle.

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.

TypeTypical useExample
byteSmall signed integersbyte level = 100;
shortSmaller-range signed integersshort year = 2026;
intGeneral whole numbersint count = 42;
longLarger whole numberslong population = 8_000_000_000L;
float32-bit floating pointfloat ratio = 1.5F;
double64-bit floating pointdouble price = 19.99;
booleantrue or falseboolean online = true;
charSingle UTF-16 code unitchar 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.

Coder Army walks through variables, identifiers, primitive data types, literals, and declaration basics in Java.

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 beginner Java discussion emphasizes variables, data types, and control flow as the fundamentals to learn before moving into object-oriented topics.

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 a String; 'A' is a char.
  • Forgetting an L on a very large long literal: the unsuffixed integer literal may be outside the int range before assignment is considered.
  • Forgetting F for a float literal: decimal floating-point literals are normally double by default.
  • Reading an uninitialized local variable: the compiler rejects it.
  • Calling String a primitive: String is 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

  1. Create a file named PrimitiveTypesDemo.java.
  2. Declare one variable for each primitive type.
  3. Print at least four of the values.
  4. Change one int to a long and add an uppercase L suffix to its literal.
  5. Create a local variable without assigning it, try to print it, and read the compiler error.
  6. Change char grade = 'A'; to String grade = "A"; and explain the type difference.
  7. Replace one obvious local declaration with var and explain what type the compiler inferred.

Knowledge Check + Answers

  1. How many primitive types does Java define? Eight.
  2. Name them. byte, short, int, long, float, double, boolean, and char.
  3. Is String primitive? No. String is a reference type.
  4. What is a literal? A value written directly in source code, such as 42, true, or 'A'.
  5. Do local variables receive automatic default values? No. They must be definitely assigned before use.
  6. Do fields receive default values? Yes, when no explicit initializer is supplied.
  7. Does var make Java dynamically typed? No. The compiler infers a static type.
  8. Which primitive type usually stores ordinary whole numbers? int.
  9. 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.

2 responses to “OSJava.002: Primitive Types and Variables — byte, short, int, long, float, double, boolean, char, Literals, and Local Variables”

  1. […] Unlike the Java lesson immediately before this one, where a variable declaration such as int age = 25; fixes the variable to an integer type, JavaScript is dynamically typed. A binding can hold a number now and a string later. That contrast is useful if you just read OSJava.002: Primitive Types and Variables. […]

    Like

  2. […] three assignments are valid. This differs from the static variable declarations introduced in OSJava.002, while it is conceptually closer to the dynamic binding behavior shown in […]

    Like

Leave a Reply