Variables & The 8 Primitive Data Types

🟢 Beginner Level | Java Core Syntax

📖 Definition

A Variable is a named location in memory used to store data values. Java is a statically typed language featuring 8 Primitive Data Types: byte, short, int, long, float, double, boolean, and char.

🌐 Multilingual Explanations

English

Primitives store raw values directly on the JVM Stack memory. Each primitive has a fixed bit size (e.g. int is 32-bit, long is 64-bit).

Hindi

Java mein har variable ka data type declare karna zaroori hota hai. Primitive types direct values store karte hain Stack memory mein.

Marathi

Java madhye pratyek variable cha type sangava lagto. Primitive types raw values Stack memory madhye jatan kartat.

Hinglish

Variables are like labeled storage boxes. `int` for whole numbers, `double` for decimals, `boolean` for true/false.

📊 8 Primitive Data Types Reference Table

Data Type Size (Bits/Bytes) Range / Values Default Value
byte8 bits (1 byte)-128 to 1270
short16 bits (2 bytes)-32,768 to 32,7670
int32 bits (4 bytes)-231 to 231-10
long64 bits (8 bytes)-263 to 263-1 (Requires L suffix)0L
float32 bits (4 bytes)Single precision floating point (Requires f suffix)0.0f
double64 bits (8 bytes)Double precision floating point0.0d
boolean1 bit (JVM dependent)true or falsefalse
char16 bits (2 bytes)Unicode characters (0 to 65,535)''

⚡ Code Example

public class PrimitivesDemo {
    public static void main(String[] args) {
        int age = 25;
        long population = 8000000000L; // 'L' suffix required for long
        float pi = 3.14159f;           // 'f' suffix required for float
        double distance = 149600000.50;
        boolean isActive = true;
        char grade = 'A';

        System.out.println("Age: " + age + ", Population: " + population);
    }
}

⚠️ Common Pitfalls

Missing Literal Suffixes: Forgetting the L suffix on large integer values or f on float numbers causes compile-time syntax errors (`integer number too large` or `possible loss of precision`).

🎯 Tiered Practice Exercises

  1. Beginner: Declare variables representing a product's price, quantity, and in-stock boolean flag.
  2. Intermediate: Demonstrate the difference in precision between float and double during calculations.
  3. Advanced: Explain how JVM Stack memory handles local primitive variable declarations.

🧭 Navigation

← Module Index | Module Hub | 02. Type Casting →