Codectionary / Developer documentation / Java

Variables & Primitive Data Types

Java is a statically-typed language, meaning every variable must be declared with an explicit type before use, and that type cannot change afterward. Java has eight primitive types - byte, short, int, long, float, double, char, and boolean - which store simple values directly rather than references, making them fast and memory-efficient. Anything beyond primitives (String, arrays, custom classes) is a reference type instead.

Syntax

type variableName = value;

Examples

Declaring Primitive Variables

Creating variables of each common primitive type.

int age = 21;
double gpa = 3.8;
char grade = 'A';
boolean isStudent = true;
long population = 8000000000L;  // L suffix for long literals
float price = 19.99f;            // f suffix for float literals

System.out.println(age + " " + gpa + " " + grade + " " + isStudent);

Constants with final

Using the 'final' keyword to declare a variable that cannot be reassigned.

final double PI = 3.14159;
final int MAX_USERS = 100;

// PI = 3.14;  // This would cause a compile error

System.out.println("Max users allowed: " + MAX_USERS);

Variable Scope

Understanding how a variable's visibility is limited to the block it's declared in.

public class ScopeExample {
    static int classField = 10;  // accessible throughout the class

    public static void main(String[] args) {
        int localVar = 5;  // only accessible inside main()

        if (localVar > 0) {
            int blockVar = 20;  // only accessible inside this if block
            System.out.println(blockVar);
        }

        System.out.println(classField + localVar);
        // System.out.println(blockVar);  // Error: out of scope here
    }
}

Default Values

Instance and static fields get default values automatically if not explicitly initialized (local variables do not).

public class Defaults {
    int number;        // defaults to 0
    boolean flag;       // defaults to false
    String text;         // defaults to null

    public static void main(String[] args) {
        Defaults d = new Defaults();
        System.out.println(d.number);  // 0
        System.out.println(d.flag);    // false
        System.out.println(d.text);    // null
    }
}

Best practices

  • Use camelCase for variable names (studentAge, not StudentAge or student_age) to follow Java conventions
  • Choose the smallest primitive type that safely fits your data - use int for whole numbers unless you specifically need long's larger range
  • Use 'final' for values that should never change after initialization, both for safety and to communicate intent to other developers
  • Prefer double over float for decimal values unless memory is a serious constraint - float has noticeably less precision
  • Always initialize local variables explicitly - unlike fields, Java will not compile code that reads an uninitialized local variable

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading