Variables & Data Types in Java

    Atul Kabra4 min readUpdated

    A variable in Java is a named box that holds a value of a declared type. Java is statically typed: every variable has a type fixed at compile time, and the compiler refuses code that puts the wrong kind of value in the wrong box. Java offers eight primitive types for raw values (numbers, characters, booleans) and reference types for everything else (objects, arrays, String). Since JDK 10 you can also use var to let the compiler infer the type for local variables.

    Declaring a variable

    int age = 19;          // declare 'age' of type int, assign 19
    double price = 249.50; // a decimal number
    boolean enrolled = true;
    String name = "Asha";  // String is a reference type, not a primitive
    

    The pattern is type name = value;. You can declare first and assign later, but you must assign a value before you read it.

    The eight primitive types

    TypeSizeHoldsExample
    byte8-bitsmall integersbyte b = 100;
    short16-bitintegersshort s = 30000;
    int32-bitwhole numbers (default)int n = 42;
    long64-bitlarge whole numberslong big = 9_000_000_000L;
    float32-bitdecimals (less precise)float f = 3.14f;
    double64-bitdecimals (default)double d = 3.14159;
    char16-bita single Unicode characterchar grade = 'A';
    booleantrue or falseboolean ok = false;
    // Commented examples of each primitive.
    long population = 9_000_000_000L;  // 'L' suffix needed: too big for int; underscores aid reading
    float ratio = 0.75f;               // 'f' suffix marks a float literal
    double pi = 3.141592653589793;     // double is the default for decimals
    char letter = 'J';                 // single quotes for a single char
    boolean passed = (population > 0); // a comparison yields a boolean
    

    Use int for whole numbers and double for decimals unless you have a specific reason not to. long and float exist for special cases; byte and short are rarely needed in everyday code.

    Reference types

    Anything that is not one of the eight primitives is a reference type: String, arrays, and every class you or the library defines. A reference variable holds a reference (a handle) to an object stored elsewhere in memory, not the object itself.

    String city = "Jalgaon";   // 'city' refers to a String object
    int[] marks = {88, 92, 75}; // an array is a reference type too
    

    A reference can be null, meaning "points at nothing". Primitives can never be null.

    Literals

    A literal is a value written directly in code: 42, 3.14, 'A', true, "hello". Helpful touches:

    int million = 1_000_000;        // underscores group digits for readability
    int hex = 0xFF;                 // hexadecimal literal = 255
    int binary = 0b1010;            // binary literal = 10
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Type inference with var

    For local variables (inside a method), var lets the compiler infer the type from the initializer. The variable is still strongly typed — var is not "any type".

    var count = 10;            // inferred as int
    var label = "Batch A";     // inferred as String
    var prices = new double[]{99.0, 149.0}; // inferred as double[]
    // var x;                  // ERROR: no initializer, nothing to infer
    

    Use var when the type is obvious from the right-hand side. Spell out the type when it improves clarity.

    Common mistakes

    • Reading an uninitialized local variable. Java will not compile code that uses a local before it is assigned.
    • Integer division surprises. 5 / 2 is 2, not 2.5, because both operands are int. Write 5.0 / 2 to get 2.5.
    • Forgetting the L or f suffix. 9_000_000_000 without L overflows int and fails to compile; 3.14 is a double, so assigning it to a float without f is an error.
    • Confusing char and String quotes. 'A' is a char; "A" is a String. They are not interchangeable.
    • Treating var as dynamic typing. Once inferred, the type is fixed; you cannot later assign a String to a var that started as an int.

    FAQ

    Is String a primitive? No. It is a class (a reference type) that gets special syntax support, which is why it can feel primitive.

    When should I use long instead of int? When values can exceed about 2.1 billion, such as timestamps in milliseconds or large counters.

    Keep going

    Next, see how to combine values with Operators in Java, or revisit installing the JDK. All tutorials live on the Java hub; for guided practice in Jalgaon, see the Java course.


    Want structured Java lessons and mentorship at Infoplanet, Jalgaon? Join the waitlist for the Java course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides