Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Java Basics

    Variables and Types

    A variable is a named box in memory that holds a value.

    Chapter 2 of 97 min read

    The Plain Picture#

    A row of labelled boxes. Each box has a name written on it ("score"), holds one value at a time (200), and — in Java — is made for one kind of thing: a box for whole numbers can't hold text, just as an egg carton can't hold soup.

    The Real Names#

    • A variable is a named box in memory that holds a value.

    • Its type says what kind of value it may hold, and therefore how many bytes it needs and how those bytes are read (How Computers Work Ch 4: bytes + an agreement).

    • Declaring a variable creates the box: int score;. Assigning puts a value in: score = 200;. Both at once: int score = 200; (initialising).

    • Java is statically typed: every variable's type is fixed when you write the code, and the compiler checks every use.

    Under the Hood#

    The 8 primitive types#

    A primitive type holds a simple value directly in the box (everything else in Java is an object, reached by a reference — Classes, Objects and Memory).

    TypeSizeHoldsRange / notesExample
    byte8 bitswhole number−128 to 127byte b = 100;
    short16 bitswhole number−32,768 to 32,767rarely used
    int32 bitswhole numberabout ±2.1 billion — the default choiceint score = 200;
    long64 bitswhole numberabout ±9.2 × 10¹⁸long views = 9_000_000_000L;
    float32 bitsdecimal~7 significant digitsfloat f = 3.14f;
    double64 bitsdecimal~15–16 significant digits — the default for decimalsdouble avg = 86.5;
    boolean(JVM-dependent)true or falsenothing else — not 0/1boolean onTime = true;
    char16 bitsone charactera UTF-16 unit, 0 to 65,535char grade = 'A';

    All the sizes and ranges come straight from How Computers Work Ch 4. Real limits:

    Java
    System.out.println(Integer.MAX_VALUE + " " + Long.MAX_VALUE);
    // 2147483647 9223372036854775807

    Literals — how you write values#

    Java
    int a = 200; // whole number literal → int
    
    long b = 9_000_000_000L; // L suffix = long. Without it, this number is too big for an int → compile error
    
    int million = 1_000_000; // underscores are ignored — just for readability
    
    double c = 86.5; // decimal literal → double
    
    float d = 86.5f; // f suffix = float
    
    char e = 'A'; // single quotes: ONE character
    
    String f = "A"; // double quotes: text (a String — Ch 4). 'A' and "A" are different types!
    
    boolean g = true;
    
    int h = 0xFF; // hex literal = 255 (01_how_computers_work.md Ch 3)
    
    int k = 0b1100_1000; // binary literal = 200

    Naming rules and conventions#

    • Names may use letters, digits, _ and $, but can't start with a digit, and can't be a keyword (int, class, for...).

    • Conventions everyone follows:

      • variables and methods: camelCase — correctAnswers, bestScore;

      • classes: PascalCase — StudyBuddyReport;

      • constants: UPPER_SNAKE_CASE — POINTS_PER_CORRECT.

    • Name for meaning: correctAnswers, not ca or x. In interviews, clear names are part of what's graded.

    Constants — final#

    Java
    final int POINTS_PER_CORRECT = 10;
    
    POINTS_PER_CORRECT = 20; // compile error: cannot assign a value to final variable

    final means "this box can be filled once and never changed". Use named constants instead of unexplained numbers in code (a 10 in the middle of a formula is a "magic number" — nobody knows what it means).

    Default values and "definitely assigned"#

    Java refuses to let you read a local variable (one declared inside a method) before you've assigned it:

    Java
    int total;
    
    System.out.println(total); // compile error: variable total might not have been initialized

    This is a safety feature: in some languages the box would contain leftover garbage bytes from earlier.

    Gotchas

    • int overflow is silent (How Computers Work Ch 4.1). If a sum might pass ~2.1 billion — adding up many scores, multiplying two large numbers — use long. This is one of the most common wrong-answer causes on LeetCode.

    • char is a number underneath, so 'A' + 1 is 66, not "B" (Ch 3).

    • Don't use float unless you have a reason (memory-heavy arrays). double is the normal decimal type — and never for money (How Computers Work Ch 4.2).

    Check yourself

    5 questions
    1

    What's the difference between declaring and initialising a variable?

    Show answer

    Declaring creates the variable with its type (int score;); initialising also gives it its first value (int score = 200;).

    2

    Which type would you use for: a quiz score; the number of video views worldwide; an average; whether a quiz is finished; a grade letter?

    Show answer

    int; long; double; boolean; char.

    3

    Why does long x = 9000000000; fail to compile?

    Show answer

    A literal without L is an int, and 9 billion doesn't fit in an int. Write 9000000000L.

    4

    What does final do?

    Show answer

    Makes a variable assignable only once — a constant.

    5

    What are the naming conventions for variables, classes and constants?

    Show answer

    camelCase for variables/methods, PascalCase for classes, UPPER_SNAKE_CASE for constants.